Jump to content

Sprite-on-sprite movement manipulation


fubeca6
 Share

Recommended Posts

Good Morning,

 

I'm trying to have my 'player' sprite be able to move 'box' sprites around the screen. So far, I can push the box... but it doesn't stop moving. The effect is like the box is on ice. I touch it and it slides away. Here's my code:

function update() {        game.physics.arcade.collide(player, block, moveBlock, null, this);}function moveBlock() {	if (block.body.touching.up) {		block.body.velocity.y = 75;	} else if (block.body.touching.down) {		block.body.velocity.y = -75;	} else {		block.body.velocity.y = 0;	}	if (block.body.touching.left) {		block.body.velocity.x = 75;	} else if (block.body.touching.right) {		block.body.velocity.x = -75;	} else {		block.body.velocity.x = 0;	}}

The problem seems to be that the 'else' of the statements is never "true". Now, I put a console.log statement in the 'else' portion for vertical movement, and noticed that the only time it ever responded was when I went from pushing the block vertically to horizontally.

 

So how I do I get the dang thing to stop moving when my player sprite is not touching it?

 

Thanks for your help!

Link to comment
Share on other sites

You need to set drag on the block's body - higher values will make the block come to a stop sooner, so something like 500 will make it stop pretty much immediately when the player stops pushing it:

block.body.drag.setTo(500, 500);

You only need to do this when creating the blocks, rather than on every update.

Link to comment
Share on other sites

Haha no problem mate! I'm not in it for the money; helping people out on here has led to me learning more about Phaser than I ever could have from just working on my own projects - it certainly makes you think outside the box when tackling other people's problems.

 

If you want to really help Phaser along I'd suggest donating to Phaser on Gittip, as it's Rich who really deserves the cash for his constant hard work on bringing such a fine framework to us all :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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