ManBoy Posted March 11, 2014 Share Posted March 11, 2014 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 More sharing options...
moe091 Posted March 11, 2014 Share Posted March 11, 2014 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 More sharing options...
ctmartinez1992 Posted March 11, 2014 Share Posted March 11, 2014 checking examples-phaser.io is smart like moe091 said. I don't exactly understand what you mean by "face" Link to comment Share on other sites More sharing options...
ManBoy Posted March 13, 2014 Author Share Posted March 13, 2014 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%20spriteThe 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 functionIf 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 More sharing options...
ctmartinez1992 Posted March 13, 2014 Share Posted March 13, 2014 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 More sharing options...
ManBoy Posted March 13, 2014 Author Share Posted March 13, 2014 I'm usingball.body.collideWorldBounds = true;instead of creating my own walls, and I don't know how to refer to those bounds Link to comment Share on other sites More sharing options...
Recommended Posts