Jump to content

A better way to control enemy movement


Recommended Posts



I have implemented a method to control enemy movement in a platform game. I have done it pretty sketchy and i'm wondering what would be a better / 'proper' way of doing it. Essentially it makes an enemy sprite walk to the end of a platform and when the sprite collides with the corner tile (before falling off the edge of the platform) the sprite then turns around and walks towards the other end of the platform and vice versa. 


I found that if I made the sprite turn as soon as it hit the corner tile that it would constantly go backwards and forwards on the spot because it was updating so quickly so I made it so it could only turn after a certain amount of time of previously turning. I know this solution is pretty sketchy but would be grateful if anyone could give me ideas as to a better way of doing this. Thank you.

this.game.physics.collide(bird, layer, this.enemyTurn, null, this);
enemyTurn: function (enemyHit, layerHit) {            if(layerHit.tile.index == 25 || layerHit.tile.index == 35 || layerHit.tile.index == 34 || layerHit.tile.index == 54 ||     layerHit.tile.index == 28 || layerHit.tile.index == 38 || layerHit.tile.index == 44 || layerHit.tile.index == 10)    {                if (enemyHit.body.velocity.x == forward && allowedToTurn == true)        {            timeWhenTurned = this.game.time.now;            allowedToTurn = false;            enemyHit.body.velocity.x = backwards;        }        else if (enemyHit.body.velocity.x == backwards && allowedToTurn == true)        {            timeWhenTurned = this.game.time.now;            allowedToTurn = false;            enemyHit.body.velocity.x = forward;                    }      }            timeSinceTurned = this.game.time.elapsedSecondsSince(timeWhenTurned);      if(timeSinceTurned > 0.06)      {        allowedToTurn = true;      }    },
Link to comment
Share on other sites

  • 2 weeks later...

couldnt you just use a transparent tile on a second tile layer and set the collisions so that only enemies collide with this layer and not your player,bullets or whatever.... that way you could define hundrets of turning points directly in 'tiled' (if you are using tiled)

as collision callback you would change the velocity of the enemy to its negative.. enemy.velo *= -1

(to change every enemies velo in seperate you of course would have to define it when you create your enemy .. enemy.velo = 200; (you can assign your own variables to a sprite.. that way your enemies can have different velocities)

to avoid changing back and forth all over again you could just set the x coordinate of the enemy back a little bit in the same step.

enemy.body.x -= enemy.velo/100

if velo is 200 this would move the body away from the colliding (turning)tile 2 pixel so the collision wouldnt fire again immediately and the enemy can walk away.. but your timer is also a good idea..

Link to comment
Share on other sites

i thought about the idea i had yesterday and decided to give it a go..   i made a new layer.. planted my "enemy turning points" on it  (a grey traffic cone) and made the enemies (only the enemies nothing else) collide with it and turn if they collide with it..  actually i programmed the enemies in a way so they would turn every time they touch anything on the right or the left so they would also turn when they collide with other elements on the map.

function moveAliveEnemy (enemy) {   if (touchingLeft(enemy.body)||touchingRight(enemy.body)){    //set "back" enemy a few pixels to not fire touchingLeft/Right again and turn speed around    enemy.body.x -= enemy.velo/100;     enemy.velo *= -1;    enemy.scale.x *=-1;   }   enemy.body.velocity.x=enemy.velo;}

you can test the outcome here.. http://test.xapient.net/phaser/ALL/


of course i would make this layer transparent in the future..   


(btw. the touchingLeft ond touchingRight functions are custom ones because i use p2 physics where there is no  body.touching.right   :(  )


and here is how i keep my enemy on a moving platform ^^





Link to comment
Share on other sites

  • 1 year later...

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...