marros1812 Posted December 29, 2015 Share Posted December 29, 2015 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 More sharing options...
Get_Bentley Posted December 30, 2015 Share Posted December 30, 2015 Not sure if this is what you are looking for but this should cause your enemies to change from -100 to 100 velocity.x when they collide. // in update function if(enemies && collisionLayer2.collide) {enemies.setAll('body.velocity.x', 100);} Let me know if this worked or helped. No way for me to test it since I am at work marros1812 1 Link to comment Share on other sites More sharing options...
marros1812 Posted December 30, 2015 Author Share Posted December 30, 2015 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 More sharing options...
lewster32 Posted December 30, 2015 Share Posted December 30, 2015 Phaser's Arcade physics already has this functionality built in via the bounce property. Something like the following should make enemies reverse totally on collisions on the x axis:enemies.setAll('body.bounce.x', 1); Link to comment Share on other sites More sharing options...
marros1812 Posted December 30, 2015 Author Share Posted December 30, 2015 Thanks lewster32, that sorted my problem. I was stuck with that for hours lol lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts