Jump to content

Problem with collisions: game.physics.collide()


dandorf
 Share

Recommended Posts

I'm trying to do the pong. And when I want to collisions, I have the following: 

 



  game.physics.collide (ball, playerBet, ballHitsBet, null, this); 


 

ballHitsBet is a function, but it gives me error then the program does not read my function, provides as follows: 

 

Uncaught TypeError: undefined is not a function 

 

What can be happening?

Link to comment
Share on other sites

Thanks for reply.

 

The complete code is as follows. 
 
I am not using states, it's all in one file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Pong</title></head><body><div id="ejemplo"></div><script src="phaser.min.js"></script><script>	var game = new Phaser.Game(640, 480, Phaser.CANVAS, 'ejemplo', { preload: preload, create: create, update: update });		var playerBet;	var computerBet;	var ball;	var computerBetSpeed = 190;	var ballSpeed = 300;	var ballReleased = false; 	var ballHitsBet;	    function preload() {        game.load.image('bet', 'assets/rocket.png');        game.load.image('ball', 'assets/ball.png');        game.load.image('background', 'assets/backg.png');    }    function create() {        game.add.tileSprite(0, 0, 640, 480, 'background');								ball = game.add.sprite(game.world.centerX, game.world.centerY, 'ball');		game.physics.startSystem(Phaser.Physics.ARCADE);		game.physics.enable(ball);		ball.anchor.setTo(0.5, 0.5);				ball.body.collideWorldBounds = true;		ball.body.bounce.setTo(1, 1);						computerBet = createBet(620, game.world.centerY);		playerBet = createBet(20, game.world.centerY);				computerBet.body.collideWorldBounds = true;			computerBet.body.bounce.setTo(1, 1);			computerBet.body.immovable = true;				game.input.onDown.add(releaseBall, this);    }    function update () {		//Control the player's racket        playerBet.x = game.input.x;        var playerBetHalfWidth = playerBet.width / 2;        if (playerBet.x < playerBetHalfWidth)		{      	    playerBet.x = playerBetHalfWidth;        } 		else if (playerBet.x > game.width - playerBetHalfWidth)		{           playerBet.x = game.width - playerBetHalfWidth;        }         //Control the computer's racket        if(computerBet.x - ball.x < -15)		{            computerBet.body.velocity.x = computerBetSpeed;        }		else if(computerBet.x - ball.x > 15) 		{            computerBet.body.velocity.x = -computerBetSpeed;        } 		else 		{            computerBet.body.velocity.x = 0;        }        //Check and process the collision ball and racket        game.physics.collide(ball, playerBet, ballHitsBet, null, this);        game.physics.collide(ball, computerBet, ballHitsBet, null, this);				checkGoal();    }			//Funciones	function createBet(x, y)	{		var bet = game.add.sprite(x, y, 'bet');			game.physics.enable(bet);		bet.anchor.setTo(0.5, 0.5);			bet.body.collideWorldBounds = true;			bet.body.bounce.setTo(1, 1);			bet.body.immovable = true;			return bet;	}			function releaseBall()	{        if (!ballReleased)		{            ball.body.velocity.x = ballSpeed;            ball.body.velocity.y = -ballSpeed;            ballReleased = true;        }    }			function ballHitsBet (_ball, _bet)	{        var diff = 0;        if (_ball.x < _bet.x) 		{            //If ball is in the left hand side on the racket            diff = _bet.x - _ball.x;            _ball.body.velocity.x = (-10 * diff);        } 		else if (_ball.x > _bet.x)		{           //If ball is in the right hand side on the racket            diff = _ball.x -_bet.x;            _ball.body.velocity.x = (10 * diff);        }		else 		{            //The ball hit the center of the racket, let's add a little bit of a tragic accident(random) of his movement            _ball.body.velocity.x = 2 + Math.random() * 8;        }    }		function checkGoal() 	{        if (ball.y < 15) 		{            setBall();        } 		else if (ball.y > 625) 		{            setBall();        }    }     function setBall() 	{        if (ballReleased) 		{            ball.x = game.world.centerX;            ball.y = game.world.centerY;            ball.body.velocity.x = 0;            ball.body.velocity.y = 0;            ballReleased = false;        }    }</script></body></html>
Link to comment
Share on other sites

Arcade is the default basic physics system - it's loosely based on the kind of physics old games used to have, with simple approximations of gravity, velocity, drag and so on. It also uses an 'AABB' (Axis Aligned Bounding Box) model for its bodies, which basically means bodies are always rectangles, and cannot be rotated. This makes it really fast but puts some obvious limits on situations where you need more realistic collisions.

 

For a much better simulation of realistic physics, P2 provides a much enhanced simulation, allowing for arbitrarily shaped bodies, constraints, forces and other cool stuff. It's not as fast as Arcade, but it's still pretty fast and was chosen I think in part because of its speed, making it viable on mobile.

 

On older versions of Phaser there was also Ninja physics which provided some extra features over Arcade such as collision with slopes, curves and circles, but it was buggy and no-one was contributing to it, so it has been removed from the standard builds of Phaser as of 2.1.0.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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