简体   繁体   English

恢复场景后如何防止精灵静止不动? 移相器 3

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

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.在暂停场景之前,我将velocity.xvelocity.y设置为 0。 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);
}

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);在我下面引用的教程中,他们让您添加一个唤醒 function 以通过将此行放入您的创建来重置 cursor 键:this.sys.events.on('wake', this.wake, this);

And then this function:然后是这个 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:因此,我在唤醒 function 时设置了 boolean:

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

and then in my Update function, I check for the bool and reset the keys there:然后在我的更新 function 中,我检查 bool 并在那里重置键:

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.就像我说的,这在我唤醒 function 时没有重置时有效。 Somewhere between my wake function running and the first update, the cursor was being updated to down, but I can't figure out where.在我唤醒 function 运行和第一次更新之间的某个时间,cursor 被更新为 down,但我不知道在哪里。

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.原始帖子我遇到了同样的问题,我认为这是最新版本的 Phaser 3 中的一些变化。

I followed this tutorial: https://gamedevacademy.org/how-to-create-a-turn-based-rpg-in-phaser-3-part-3/我遵循了本教程: 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.本教程提到了这个问题,场景恢复后精灵正在移动,并添加了唤醒 function 以重置 cursor 键。 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.我从教程中下载了源代码,发现它没有这个问题,但我注意到源代码带有一个 phaser.min.js,而我在我的 html 文件中引用了在线的最新版本。 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在我的 playScene 中添加一个暂停事件

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在我的播放器 class 中创建了一个 function 来重置场景暂停时调用的所有键值

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可能有更好的方法来处理这个问题,但对于我现在想要的,它是有效的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM