问题描述
正如标题所述。从“游戏”对象开始场景时,是否可以停止所有正在运行的场景?
目前,当我从Scene对象外部调用game.scene.start(...)
时,什么都没有发生。我当前的解决方案是使全局变量指向最近启动的场景(在我所有的场景中,我都在create()
函数中使用诸如currentScene = this;
之类的值来重置该全局变量)因此,当我要启动我称为currentScene.scene.start(...)
的新场景。但这显然是不可靠的,因为我可能会忘记在所有场景中重设全局变量(currentScene
)。
编辑:
为了澄清我要达到的目标:我正在制作一个带有几个场景和几个GameObjects的小型多人游戏。我有一个“套接字”模块,用于处理客户端和服务器之间的所有流量(我正在使用socket.io)。我想做类似的事情:
this.socket.on('game_end',(data) => {
// Server decides when the game stops and sends an event to all clients.
// Here I need to stop the currently running scene (i.e. SceneInGame)
// and show the "game finished / results" scene.
this.game.scene.start('SceneGameEnd');
// ^ That doesn't work. SceneInGame keeps playing and SceneGameEnd doesn't start
});
解决方法
您真的不应该在Phaser 3中完全使用game.scene
(实际上,从Phaser 3.50.0开始甚至不起作用)。场景操作应通过ScenePlugin
进行,this.scene
可从任何场景中以this.scene.start('otherScene')
的形式进行访问。即game.scene.start
将停止当前场景,然后在下一个游戏步骤中按顺序开始给定场景。 launch
在哪里播放,而无需关心游戏步骤或调用它的内容。
但是没有办法“停止所有正在运行的场景”,但是除非您已经启动了多个场景(通过checkpoint_path = "training_2/cp-{epoch:04d}.ckpt" # <<-- this {epoch:04} thing
cp_callback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_path,verbose=1,save_weights_only=True,period=5)
model.fit(train_images,train_labels,epochs=50,callbacks=[cp_callback],validation_data=(test_images,test_labels),verbose=0)
或类似方法),否则实际上不会是任何其他正在运行的场景。显示一些实际代码,然后我们可以给出更有针对性的响应。