场景更改 Phaser 3 中的 setInterval 是否被清除?
Is setInterval cleared on scene change Phaser 3?
Phaser 3 场景变化时所有的 setIntervals 都被清除了吗?例如,如果我有这个代码:
class ExampleScene extends Phaser.Scene {
constructor () {
super();
}
preload () {
}
create () {
setInterval(() => console.log(true), 1000);
}
update () {
}
}
我换个场景,它会继续向控制台记录true
吗? Phaser 中是否有不需要我手动删除所有间隔的替代方案?
简短的回答是,否。因为 setInterval
是一个 javascript 函数。
函数详情:here in the documentation on mdn
您可以做的是将 setInvertal
calls/id“保存”在列表中,并在特定的 scene
事件中清除它们,例如 shutdown, 暂停, 毁灭 ...当该事件触发时,您可以停止所有保存的间隔,或者如此 (details to possible Scene events)。
这也被认为是良好做法,因为当离开移相器场景时,您总是应该清理资源。
示例(这里有关机事件):
...
// setup list for the intervals, that should be cleared later
constructor () {
super();
this.intervals = [];
}
create () {
...
// example of adding an interval, so that I can be cleanup later
this.intervals.push(setInterval(() => console.log(true), 1000));
...
// example listening to the shutdown Event
this.events.on('shutdown', this.stopAllIntervals, this);
}
...
// example "cleanup"-function, that is execute on the 'shutdown' Event
stopAllIntervals(){
for(let interval of this.intervals){
clearInterval(interval);
}
}
...
现在您只需在需要的事件函数中调用 stopAllIntervals
,当您想要停止它们时。
From the offical documenation, on the shutdown Event: ... You should free-up any resources that may be in use by your Scene in this event handler, on the understanding that the Scene may, at any time, become active again. A shutdown Scene is not 'destroyed', it's simply not currently active. Use the DESTROY event to completely clear resources. ...
Phaser 3 场景变化时所有的 setIntervals 都被清除了吗?例如,如果我有这个代码:
class ExampleScene extends Phaser.Scene {
constructor () {
super();
}
preload () {
}
create () {
setInterval(() => console.log(true), 1000);
}
update () {
}
}
我换个场景,它会继续向控制台记录true
吗? Phaser 中是否有不需要我手动删除所有间隔的替代方案?
简短的回答是,否。因为 setInterval
是一个 javascript 函数。
函数详情:here in the documentation on mdn
您可以做的是将 setInvertal
calls/id“保存”在列表中,并在特定的 scene
事件中清除它们,例如 shutdown, 暂停, 毁灭 ...当该事件触发时,您可以停止所有保存的间隔,或者如此 (details to possible Scene events)。
这也被认为是良好做法,因为当离开移相器场景时,您总是应该清理资源。
示例(这里有关机事件):
...
// setup list for the intervals, that should be cleared later
constructor () {
super();
this.intervals = [];
}
create () {
...
// example of adding an interval, so that I can be cleanup later
this.intervals.push(setInterval(() => console.log(true), 1000));
...
// example listening to the shutdown Event
this.events.on('shutdown', this.stopAllIntervals, this);
}
...
// example "cleanup"-function, that is execute on the 'shutdown' Event
stopAllIntervals(){
for(let interval of this.intervals){
clearInterval(interval);
}
}
...
现在您只需在需要的事件函数中调用 stopAllIntervals
,当您想要停止它们时。
From the offical documenation, on the shutdown Event: ... You should free-up any resources that may be in use by your Scene in this event handler, on the understanding that the Scene may, at any time, become active again. A shutdown Scene is not 'destroyed', it's simply not currently active. Use the DESTROY event to completely clear resources. ...