bloodbarron115 Posted March 19, 2015 Share Posted March 19, 2015 I have 3 opponents spawning in and occasionally they spawn on eachother. How can I have it check if an opponent has already spawned there? for(var i=0; i<3; i++) {opponent = this.createOpponents();} createOpponents: function () {var rndPlace = Math.floor(Math.random() * (188 - 0)) + 0;opponent = this.game.add.sprite(0, rndPlace, 'enemy');this.game.physics.enable(opponent, Phaser.Physics.ARCADE);var rndVel = Math.floor(Math.random() * (10 - 5)) + 5;opponent.body.velocity.x = rndVel;return opponent;} Link to comment Share on other sites More sharing options...
ZoomBox Posted March 19, 2015 Share Posted March 19, 2015 I suggest you to use Phaser.Math.distance(x1, y1, x2, y2);http://docs.phaser.io/Phaser.Math.html#distance So basically you do this:Each time you created an opponent, store it in an Array.Each time you create a new opponent, browse your list of all already-existing opponent and check with Math.distance() if the opponent coordinates you want to create is not too close to another opponant in the Array. Second choice:You do the same but instead of checking the distance, you check if they're not overlapping with opponent.overlap(oponnents). Link to comment Share on other sites More sharing options...
mwatt Posted March 19, 2015 Share Posted March 19, 2015 Keeping track of where they spawned and then avoiding the same spot is also the first thing I thought of. But why not instead set it up so that each opponent spawns within a separate random range? You'd calculate the range so that it would be impossible to overlap. Link to comment Share on other sites More sharing options...
Recommended Posts