Jump to content

Breakout Collision of ball & paddle not working properly.


firesofmay
 Share

Recommended Posts

Hi,

 

I am trying to learn Phaser by following the breakout demo game available on the examples site.

It works, except that when I hit the paddle to the right wall,

after that the ball just passes through the left portion of the paddle.

But it does bounce off the right side of the paddle.

 

Note, that If I don't hit the right wall it works!

 

To reproduce the problem:

- Download the minimal bug reproducible code of the game attached as zip.

- Start the game.

- Hit the ball few times to see it works (left and right portion of the paddle)

- Now Hit the paddle to the right wall.

- & try hitting the ball with the left portion of the paddle, it'll just pass through!

 

Info:

- Phaser Version - 2.01

- Chrome 35.0.1916.114 (Tried on Firefox as well)

- Mac OSX 10.8.5

 

I've pasted the code inline below as well.

 

Any help would be really great!

Thanks!

var game = new Phaser.Game (800, 600, Phaser.AUTO, 'game_div', { preload: preload, create: create, update: update});var paddle;var ball;var ballOnPaddle = true;var s;function preload () {  game.load.atlas ('breakout', 'assets/breakout.png', 'assets/breakout.json');  game.load.image ('starfield', 'assets/starfield.jpg');}function create () {  game.physics.startSystem (Phaser.Physics.ARCADE);  game.physics.arcade.checkCollision.down = false;  s = game.add.tileSprite (0,0,800,600, 'starfield');  /**PADLE SECTION**/  paddle = game.add.sprite(game.world.centerX, 500, 'breakout', 'paddle_big.png');  paddle.anchor.setTo(0.5, 0.5);  game.physics.enable (paddle, Phaser.Physics.ARCADE);  paddle.body.CollideWorldBounds = true;  paddle.body.bounce.set (1);  paddle.body.immovable = true;  /*BALL SECTION*/  ball = game.add.sprite (game.world.centerX, paddle.y - 16, 'breakout', 'ball_1.png');  ball.anchor.set (0.5);  ball.checkWorldBounds = true;  game.physics.enable (ball, Phaser.Physics.ARCADE);  ball.body.collideWorldBounds = true;  ball.body.bounce.set (1);  game.input.onDown.add (releaseBall, this);}function update () {  paddle.body.x = game.input.x;  if (paddle.x < 24)  {    paddle.x = 24;  }  else if (paddle.x > game.width - 24)  {    paddle.x = game.width - 24;  }  if (ballOnPaddle)    ball.body.x = paddle.x;  else {    game.physics.arcade.collide (ball, paddle, ballHitPaddle, null, this);  }}function releaseBall () {  if (ballOnPaddle) {    ballOnPaddle = false;    ball.body.velocity.y = - 300;    ball.body.velocity.x = -75;  }}function ballHitPaddle (_ball, _paddle) {    var diff = 0;    if (_ball.x < _paddle.x)    {        diff = _paddle.x - _ball.x;        _ball.body.velocity.x = (-10 * diff);    }    else if (_ball.x > _paddle.x)    {        diff = _ball.x -_paddle.x;        _ball.body.velocity.x = (10 * diff);    }    else    {        _ball.body.velocity.x = 2 + Math.random() * 8;    }}

index.html

<!DOCTYPE html><html><head>  <meta charset="utf-8" />  <title>Empty project</title>  <script type="text/javascript" src="phaser.min.js"></script>  <script type="text/javascript" src="main.js"></script></head><body>  <div id="game_div"> </div></body></html>

try-breakout.zip

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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