Hi there,
First-time poster, first-time Phaser user here.
I have a ‘boat’, which is a physics enabled (Arcade) container that contains a “platform” physics-enabled rectangle. Both container and the platform within it have Y gravity at 0 and are immovable (so that the player does not push them).
The boat just sits in the middle of the screen until the player wishes to move it by modifying a ‘power’ variable (done via a button press) - it moves towards the right; like so;
Boat.Update() {
this.body.acceleration.x += this.power;
// "Friction".
this.body.velocity.x *= .7;
// No reversing please!
if(this.body.velocity.x < 0)
this.body.velocity.x = 0;
}
}
I also have a ‘player’, physics enabled. Platformer style movement pretty much ripped straight from the Phaser3 platform tutorial.
When the player collides with the boat, a flag is set (player ‘Z’ is set to zero when colliding with the boat).
Everything works well, until I start adding acceleration to the boat. No matter what I do, the player tends to slide along the boat’s surface (as the boat moves beneath them).
I’ve tried setting body friction to 1 on both the boat and the player. No effect.
I’ve tried " player.body.velocity.x = boat.body.velocity.x " in an Update method. This almost works; it reduces the slide but nevertheless the slide is still there.
I’ve tried doing that same thing in “Pre/Post” Update methods (after having registered them). No change.
The player movement is handled by the following method, called in Scene Update when appropriate key is pressed.
Player.Move(_dir) {
if(_dir == -1)
this.body.velocity.x = -160;
else if(_dir == 1)
this.body.velocity.x = 160;
else
this.body.velocity.x = this.z == 0 ? this.boat.body.velocity.x : 0;
}
You can see that I’m attempting to set the velocity depending on the flag in that method too. Doesn’t seem to make a difference whether I do that ternary check or just set 0 whenever _dir == 0.
I’ve even tried setting the player’s gravity really high when they’re colliding with the platform;
Player.CollisionVsPlatform(_player, _platform) {
if(_platform.body.moves && _platform.body.touching.up && _player.body.touching.down)
_player.body.gravity.y = 10000;
}
The method is called but does not appear to do anything.
I’ve uploaded the entire thing here: http://www.whoshotdk.co.uk/phaser/
The ‘boat’, ‘player’ and other scripts are here: http://www.whoshotdk.co.uk/phaser/Asset/GameScene/
The main scene file resides there too, it’s called GameScene.scene.
The game: http://www.whoshotdk.co.uk/phaser/game.html
In order to make the boat move, jump the player up onto it (up arrow), walk into the red box, then left-click the ‘+’ button that appears. You’ll see the boat move and the player slide. Note; if you cant jump high enough, make the window smaller and refresh. I haven’t got round to handling different screen sizes yet!
I’d appreciate any advice on this!
Thanks
Dave