Jump to content

Revers Velocity When Group Objects Collide


marros1812
 Share

Recommended Posts

I have managed to get my enemies to spawn and move -100 on the x axis and collide with invisible boxes. I am trying to figure out how to get the enemies to reverse and go in the opposite direction once they have collided with the boxes.

 

This is my code to add the enemies and get them moving:

enemies = game.add.group();enemies.enableBody = true;map.createFromObjects('Object Layer 1', 146, 'enemy', 1, true, false, enemies);enemies.setAll('body.velocity.x', -100);

and in the update function I have this to collide with the invisible boxes:

game.physics.arcade.collide(enemies, collisionLayer2);

I am using a tilemap that I created using tiled, if anyone has any ideas or needs further info please feel free to contribute/ask

Link to comment
Share on other sites

Thanks for the help Get_Bentley. I was able to use the part enemies.setAll('body.velocity.x', 100); to make a function that is called when the collision is detected, although I should have been more clear in my first post, I need it to work in both directions so that when it collides with a box it will reverse, direction continue until it hits the next box and reverse again and continue until the enemy is destroyed.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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