lobsterhands Posted May 2, 2014 Share Posted May 2, 2014 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 More sharing options...
Chris Posted May 2, 2014 Share Posted May 2, 2014 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 lobsterhands 1 Link to comment Share on other sites More sharing options...
lobsterhands Posted May 2, 2014 Author Share Posted May 2, 2014 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 More sharing options...
valueerror Posted May 3, 2014 Share Posted May 3, 2014 http://test.xapient.net/phaser/movingplatform.html this would be another way to go (using p2 in that case but it should work in arcade too) in p2 physics the friction is needed so the player would not slide of the platform lobsterhands 1 Link to comment Share on other sites More sharing options...
valueerror Posted May 3, 2014 Share Posted May 3, 2014 http://test.xapient.net/phaser/movingplatform-withouttweens.html here is a better approach without tweens.. it's better because you can exactly define the distance the platform will move... lobsterhands 1 Link to comment Share on other sites More sharing options...
lobsterhands Posted May 3, 2014 Author Share Posted May 3, 2014 valueerror, thanks for the examples! I'll study the code after I'm done studying finals *grumble grumble degree*. Much appreciated! Link to comment Share on other sites More sharing options...
Recommended Posts