Search the Community
Showing results for tags 'distancebetween group'.
-
Hey. I want to trigger different events depending on the distance between a member of a group (enemies) and my avatar (player). Basically, I want the enemy to 'aggro' if it's < 200 distance, and 'kill' my player if the distance is < 50. I tried to achieve this with 'arcade.overlap()' and 'arcade.collide()' but I can't seem to trigger different events based on the 'depth of the collision' (offset?). var game = new Phaser.Game(640, 960, Phaser.AUTO, '', { preload: preload, create: create, update: update});// GLOBALSvar enemies;var robot;var player;var text;var counter = 0;var xPos; // randomly generated x coordinate of robotfunction preload() { game.load.image('player', 'assets/laserpoint.png'); game.load.image('robot', 'assets/tewst.png'); }function create() { game.physics.startSystem(Phaser.Physics.ARCADE); // ENEMIES GROUP enemies = game.add.group(); enemies.enableBody = true; enemies.physicsBodyType = Phaser.Physics.ARCADE; enemies.createMultiple(20,'robot'); // PLAYER AVATAR player = game.add.sprite(game.world.width/2,game.world.height /2,'player'); game.physics.enable(player, Phaser.Physics.ARCADE); player.anchor.setTo(0.5); // COUNTER ++ IF COLLISION OCCURS text = game.add.text(game.world.centerX, game.world.centerY - 300, 'Counter: 0', { font: "64px Arial", fill: "#ffffff", align: "center" }); text.anchor.setTo(0.5, 0.5); // LOOPED TIMER SPAWNS MOB this.timer = this.game.time.events.loop(500, spawn, this); }function update() { // PROBLEM if (game.physics.arcade.distanceBetween(enemies,player) < 100) { text.setText('Counter: ' + counter); counter++; } // I've tried (robot,player) too, no luck. } // SPAWNS ENEMY ROBOT AT RANDOM X COORDINATEfunction spawn() { xPos = game.rnd.integerInRange(1,510); robot = enemies.getFirstExists(false); if(robot){ robot.reset(xPos,200); robot.body.velocity.y = 200; robot.anchor.setTo(0.5); }}