c# - Game can't recognize enemy rectangle inside platform rectangle -
so tried make so; if enemies left side position more platforms right side, enemy turns other way, , vice versa. problem game can't recognize it. enemy jumps in air @ same time, made check x values. (right , left sides of rectangles)
here code:
these checks if it's inside of platform...
static class rectanglehelper { public static bool isinsideof(this rectangle r1, rectangle r2) { return (r1.right >= r2.left && r1.left <= r2.right); } } if (!chompball.rectangle.isinsideof(platform.rectangle)) { if (chompball.movingright == true && chompball.canchangedirection == true) chompball.movingright = false; else if (chompball.movingright == false && chompball.canchangedirection == true) chompball.movingright = true; chompball.canchangedirection = false; } i have tried change lot of things, removed ! see if worked, no. have no idea! funny thing works when it's touching ground(my other enemies), x coordinate matters, doesn't.
please help!
-edit:
split logic? mean doing this?:
if (!chompball.rectangle.isinsideof(platform.rectangle) && chompball.movingright == true) chompball.movingright = false; else if (!chompball.rectangle.isinsideof(platform.rectangle) && chompball.movingright == false) chompball.movingright = true; -edit 2: oh think know mean. make on rectanglehelper checks if it's right, , vice versa. still doesn't work, result same:
if (chompball.rectangle.isrightof(platform.rectangle) && chompball.movingright == true) chompball.movingright = false; else if (chompball.rectangle.isleftof(platform.rectangle) && chompball.movingright == false) chompball.movingright = true; edit 3, enemy movements. can see, here "canchangedirection bool used. it's ment do; when enemy reached end of platforms, turns , can't turn again until 100ms. used doesn't stuck @ corner:
if (movingright == true) { position.x += 2; rotation += 0.05f; } else if (movingright == false) { position.x -= 2; rotation -= 0.05f; } if (canchangedirection == false) { timer += (float)gametime.elapsedgametime.totalmilliseconds; if (timer >= 100) { canchangedirection = true; timer = 0; } }
Comments
Post a Comment