Jump to content

Collision Callback


ManBoy
 Share

Recommended Posts

Hi, first of all, I'm a newbie in HTML5 Games Development, JavaScript and Phaser.

 

I would like to know how to get the face of the object it hit on when colliding.

Ball = function(game,x,y){	Phaser.Sprite.call(this,game,x,y,'Atlas','Ball.png');	this.anchor.setTo(0.5,0.5);	this.body.bounce.setTo(1,1);	this.body.collideWorldBounds = true;	this.body.collideCallBack = ballHit;}Ball.prototype = Object.create(Phaser.Sprite.prototype);Ball.prototype.constructor = Ball;function ballHit(face,body,other){	console.log('BOING');	console.log(face);}

The event was never fired.

Link to comment
Share on other sites

do you mean the phase as top/left/right/bottom? 

 

If so you can use body.touching.left or body.touching.top to check which side of your body is touching something.

 

e.g if body.touching.left == true then the left side of your sprite is hitting something.

 

 

if you mean you just want to find the actual object that your are colliding with, you can use the game.physics.collide function like in this example :

http://examples.phaser.io/_site/view_full.html?d=collision&f=sprite+vs+sprite.js&t=sprite%20vs%20sprite

 

or go to examples.phaser.io for more examples

Link to comment
Share on other sites

if you mean you just want to find the actual object that your are colliding with, you can use the game.physics.collide function like in this example :

http://examples.phaser.io/_site/view_full.html?d=collision&f=sprite+vs+sprite.js&t=sprite%20vs%20sprite

The thing is in that example I need to know what is the 'sprite2' in update function.

 

I want to have a general collision callback for the ball when it collides with anything, and then check which 'face' of the ball it hit on with other object, so I can tween scale the x and y of the ball to create a 'bouncy' effect.

 

There is this function below I found in the docs, but I'm not sure how to use it.

 

Name Type Description collideCallback function

If set this callback will be fired whenever this Body is hit (on any face). It will send three parameters, the face it hit on, this Body and the Body that hit it.

 

I have another slightly different question. I have ball.body.collideWorldBounce set to true. But how can I check when the ball actually hits with the walls? LIke some kind of callback.

 

Since I can't find a way for a general collision callback for the ball, I did a workaround and just added the check for the ball.body.touching.up, down etc inside all callbacks fired when colliding with other sprites.

 

But I don't know how to retrieve the walls or bounds. 

game.physics.collide(ball,worldBounds,ballHitWall,null,this);
Link to comment
Share on other sites

If you want to detect if the ball collided with any of the walls and react to it, here's a snippet:

game.physics.collide(sprite, group, collisionHandler, null, this);function collisionHandler (player, wall) {  //Do something}

I am assuming that you are creating and storing all the walls of the level in a Phaser.group

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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