How to prevent sprite stills moving after resume a scene? Phaser 3

Viewed 639

everytime I resume some scene, the main character keeps walking along, because I think it's remember the movement from before the scene was paused. I set the velocity.x and velocity.y to 0 before pause the scene. But doesn't works. Any idea? Thanks!

Here is my code:

function workPopUp (){
    this.scene.pause();
    this.scene.launch('popup');
    this.work.destroy();
}

This is the normal behaivor of velocity:

if (this.cursorKeys.left.isDown)
     {
       this.kid.setVelocityX(-200);
       this.kid.anims.play('left', true);
     }
else if (this.cursorKeys.right.isDown)
     {
         this.kid.setVelocityX(200);
         this.kid.anims.play('run', true);
    }
else
     {
    this.kid.setVelocityX(0);
    this.kid.anims.play('idle', true);
    };
if (this.cursorKeys.up.isDown && this.kid.body.touching.down)
{
    this.kid.setVelocityY(-480);
    this.kid.anims.play('jump', true);
}
if(this.cursorKeys.up.isDown && this.cursorKeys.right.isDown){
    this.kid.anims.play('jump', true);

}else if(this.cursorKeys.up.isDown && this.cursorKeys.left.isDown){
    this.kid.anims.play('jump', true);
}
2 Answers

UPDATE I still don't know why it was happening, so I'd love if someone else could find a better way, but I did find a way to stop it.

In the tutorial I reference below, they had you add a wake function to reset the cursor keys by putting this line into your create: this.sys.events.on('wake', this.wake, this);

And then this function:

wake: function() {
    this.cursors.left.reset();
    this.cursors.right.reset();
    this.cursors.up.reset();
    this.cursors.down.reset();
},

As I said, that didn't work for me. So instead, I set a boolean in my wake function:

wake: function() {
    this.sleeping = true;
},

and then in my Update function, I check for the bool and reset the keys there:

update: function (time, delta)
{
if (this.sleeping)
{
    this.sleeping = false;
    this.cursors.left.reset();
    this.cursors.right.reset();
    this.cursors.up.reset();
    this.cursors.down.reset();
}

Like I said, this works when putting the reset in my wake function didn't. Somewhere between my wake function running and the first update, the cursor was being updated to down, but I can't figure out where.

Hope this helps.

Original Post I'm having the same problem and I think it's some change in the a recent version of Phaser 3.

I followed this tutorial: https://gamedevacademy.org/how-to-create-a-turn-based-rpg-in-phaser-3-part-3/

The tutorial mentions having this problem where the sprite is moving after the scene resumes and adding an on wake function to reset the cursor keys. That wasn't working for me.

I downloaded the source code from the tutorial and found it didn't have this issue, but I noticed that the source code comes with a phaser.min.js whereas I was referencing the latest version online in my html file. When I updated the tutorial source code to use the latest version, the issue occurred.

Hope this context helps, I'll continue to troubleshoot and update if I find the issue.

I had similar problem with in my game when pausing and resuming the scene, so if I was pressing a key and paused the game, after resuming the player would continue the movement event if I was not pressing the key anymore.

So this is what I did

in my playScene add an event on pause

this.sys.events.on("pause, () => { this.player.sleep() })       

in my player class created a function to reset all the key values that is called whenever the scene is paused

sleep() {
      this.keys.left.reset();
      this.keys.right.reset();
      this.keys.up.reset();
      this.keys.down.reset();
      this.keys.jump.reset();
      this.keys.fire.reset();
    }

Probably there is a better way of handling this, but for what I want at the moment it works

Related