我在 Phaser 中使用以下函数来创建 float 指令,该指令在前 5 秒后消失。该函数是从 create
函数内部调用的。
setupText: function () {
this.instructions = this.add.text( 510, 600,
'Use Arrow Keys to Move, Press Z to Fire\nClicking does both',
{font: '20px monospace', fill: '#fff', align: 'center'}
);
this.instructions.anchor.setTo(0.5, 0.5);
this.time.events.add(5000, this.instructions.destroy, this);
},
很快就发现定时事件不是我应该在这里使用的,因为五秒后它会导致整个游戏变得无响应。
作为一个完全的初学者,我四处寻找实现此类事件的正确方法,但我找不到任何东西。
我正在遵循的教程创建一个变量,用于保存消息应保留的秒数,然后从 update
调用一个函数,该函数会在达到时间限制时销毁文本。对我来说,这似乎是一个糟糕的方法,因为检查函数会从 update
中一遍又一遍地调用,即使它只在前 5 秒内需要。
因此,我正在寻求有关最佳方法的帮助,以及为什么我的代码导致整个游戏停止的解释。
最佳答案
问题是当你调用这个时:
this.time.events.add(5000, this.instructions.destroy, this);
它调用 this
对象上的 destroy
函数,这可能是您的游戏。
要解决这个问题,请传递 this.instructions
。
this.time.events.add(5000, this.instructions.destroy, this.instructions);
关于javascript - 移相器 : destroying text after a fixed number of seconds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25681695/