Jump to content

How to Ride a Moving Sprite?


lobsterhands
 Share

Recommended Posts

Does anyone have an idea of how my player can "ride" a moving sprite. I want to jump on a horizontally moving block and my player to remain fixed in place on the block. Currently (with simple collision working) the block moves out from under the player.

 

If anyone can point me in the right direction, I'd be grateful.

Link to comment
Share on other sites

If the player collides with a moving sprite, switch it to a "follow mode" and update the players x-position together with the sprites x-position. If the player jumps and the collision is resolved, the follow mode is disabled again.

 

If you want to be tricky, use acceleration instead of plain x-position copying, so the player remains at the moving sprites' speed when he jumps into the air ;)

Link to comment
Share on other sites

Chris, I got it working using:

  function onBlock (obj1, obj2) {    if (this.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {      this.player.body.acceleration.x = -this.ACCELERATION;    } else if (this.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {      this.player.body.acceleration.x = this.ACCELERATION;    } else {      this.player.body.x = block.body.x+10; // effectively centers the player on this block    }  }

It seems clunky, but it works so.... situation: improved.

 

If I don't include the +10 to the block.body.x position, it just drops the player as far left on the block as possible, which I don't want. I'd rather the player stick where he lands.

 

Anything you would add/change?

 

Also, if you're interested, check out level 3 to see this in action: http://lyledenman.com/phaser/roy/

It's a first project. ;)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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