Jump to content

distanceBetween group, sprite?


Boxtufty
 Share

Recommended Posts

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);	}}
Link to comment
Share on other sites

distanceBetween is for checking the distance of 2 Sprites. Although you can pass in a Group, the Group x/y coordinates are likely to still be 0,0 unless you've changed them (which you haven't in the code above). So you're always checking the distance between the sprite and 0,0 - hence the weird results.

 

You need to either select a specific object in your Group for the test, or maybe loop through all of the children in the group and average out their coordinates and compare against that instead.

Link to comment
Share on other sites

Thanks for the reply, sorry I am a beginner and unable to comprehend some of what you said. 

 

How can I manipulate individual sprites in the group? When I spawn them I can set a velocity etc, but once they are let loose on the world, how can I refer to a specific enemy? The variable it was assigned to on creation has since been 'overwritten'(?) ... :blink:

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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