Reputation: 319
I'm working with the Phaser JS game framework for the first time. I'm trying to determine when two sprites overlap or collide. Here is how I'm trying to do so:
In the update function:
update: function() {
this.game.physics.collide(this.player1, this.player2, this.CollisionD, null, this);
this.game.physics.overlap(this.player1, this.player2, this.OverlapD, null, this);
}
Then in my CollisionD
function, which is my collision handler, I've tried:
function CollisionD(obj1, obj2) {
alert('collision!');
}
And I tried:
function CollisionD(player1, player2) {
alert('collision!');
}
The same goes for my overlap detection. What am I doing wrong? There are no error messages that show up in the console either.
Upvotes: 1
Views: 2956
Reputation: 681
Okay so I have had similar issues with Phaser overlap in the past and it has never seemed to work correctly for me the way I have seen in guides. So instead of passing the callback I have just used the overlap as a boolean value and used an if statement to call the method if its true. In your case that would look like:
if(this.game.physics.overlap(this.player1, this.player2))
overlapD(this.player1, this.player2);
Sure it takes one more line but it saves the hassle of broken code, right?
Upvotes: 2