amnesiaaa Posted August 21, 2016 Share Posted August 21, 2016 Hello Guys, We are trying to make a 2D fighting game, where the enemies come towards you an when they see you, they are surrounding the player. I can`t get my Enemies follow my Player after they walk towards him.. when they are reaching a distance like 200px they should get around him...but they just ignore the command. With one enemy it works, then we made a group and it doesn´t work. Here is my Code: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Phaser - Making your first game, part 1</title> <script src="//cdn.jsdelivr.net/phaser/2.2.2/phaser.min.js"></script> <style type="text/css"> body { margin: 0; } </style> </head> <body> <script type="text/javascript"> var game = new Phaser.Game(1920, 1080, Phaser.AUTO, '', { preload: preload, create: create, update: update}); function preload() { this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; this.scale.pageAlignHorizontally = true; this.scale.pageAlignVertically = true; this.scale.setScreenSize( true ); game.load.image('map', 'assets/map.png'); game.load.image('champ', 'assets/champ.png'); game.load.image('zombie', 'assets/zombie.png'); } var champ; // zombies; var zombie; function create() { game.physics.startSystem(Phaser.Physics.ARCADE); map = this.game.add.tileSprite(0, 0, 1920, 1080, 'map'); map.height = game.height; map.width = game.width; champ = game.add.sprite(100,100,'champ'); champ.scale.setTo(0.4,0.4); zombies = game.add.group(); zombies.enableBody = true; zombies.scale.setTo(0.4,0.4); zombies.physicsBodyType = Phaser.Physics.ARCADE; createZombies(); //this.zombie = game.add.sprite(1000, game.world.randomY, 'zombie'); game.camera.follow(champ); cursors = game.input.keyboard.createCursorKeys(); } function update() { if(cursors.left.isDown){ if(champ.x != 0){ champ.x -=4 } }else if (cursors.right.isDown){ if(champ.x != 200){ champ.x +=4 }else{ map.tilePosition.x -= 4; } } if(cursors.up.isDown){ if(champ.y != 0){ champ.y -= 4; } }else if(cursors.down.isDown){ if(champ.y != 600){ champ.y += 4; } } game.world.forEach(function(zombies){ if((game.physics.arcade.distanceBetween(this.champ, this.zombies)) > 200){ if(this.zombies.x == this.champ.x){ this.zombies.velocity.x = 0; }else{ if(this.zombies.x > (this.champ.x + 100)){ this.zombies.x -= 2; } if(this.zombies.x < (this.champ.x + 100)){ this.zombies.x += 2; } if(this.zombies.y > this.champ.y){ this.zombies.y -= 2; } if(this.champ.y > this.zombies.y){ this.zombies.y += 2; } } }else{ this.zombies.setAll('body.velocity.x', -200); this.zombies.setAll('body.velocity.y', 0); } }); } function createZombies(){ for(var i = 0; i < 5 ; i++){ var zombie = zombies.create(2400, game.world.randomY, 'zombie'); zombie.anchor.setTo(0.4,0.4); } } </script> </body> </html> Link to comment Share on other sites More sharing options...
Taggrin Posted August 25, 2016 Share Posted August 25, 2016 You check for each zombie in the group whether they are close to the player. If they are not close to a player you give them a default speed by using 'setAll'. However, the zombies that are busy chasing the player also fall under ALL, so their chasing will be overwritten by the else statement, making them not chase the player anymore (hence why one zombie works but multiple do not). You can fix this by simply setting the velocity of the current zombie (instead of all) to default when the player is not nearby (similar to the chasing), as you are looping through the group anyway. Link to comment Share on other sites More sharing options...
Recommended Posts