Jump to content

In arcade mode, pushing objects around the level


Recommended Posts

So, I'd like to do something like this: there are some boxes in the arcade game level. The main character can push them and move them around the level. Basically, when he collide with a box then the box is pushed by the main character.


To experiment this, I am modifying this introductory Phaser game: http://www.photonstorm.com/phaser/tutorial-making-your-first-phaser-game

What I am trying to make is this: at first the stars are not moving; when the player collides a star, then it is pushed by him. To do this, I modified the collectStar (player, star) as following:

function collectStar (player, star) {     star.body.center.x= player.body.center.x + sign(player.body.velocity.x)*player.body.halfWidth;}

This solution does not work (stars are not moving at all) and I do not like it, since the star (i guess) will witch from one side of the player to another one as the player changes direction.


So, how can I let an object be pushed by a player?

Link to comment
Share on other sites

change the following line: 

game.physics.arcade.overlap(player, stars, collectStar, null, this);

to this:

game.physics.arcade.collide(player, stars, collectStar, null, this);

and remove your custom code in collectStar.


Overlap merely checks if the two items overlap each other. collide applies physics.

Link to comment
Share on other sites

  • 2 years later...
On 18.7.2014 at 7:10 PM, lewster32 said:

You'll want to apply some drag to pushable objects too, otherwise they'll keep moving until they're stopped by another object.

box.body.drag.setTo(10000); // a very high value will make the box stop pretty much straight away when you stop pushing


It's not working, i'm using Phaser 2.6.2, anyone help me?






box.setAll('body.mass','body', 1);

Worked for me.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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