效果展示
关键技术点
- 使用
this.scene.start
打开新场景 - 打开新场景之前要先设置场景不可见,使用
this.scene.setVisible(false)
- 打开新场景后要设置场景可见,使用
this.scene.setVisible(true)
部分主要文件代码
BootScene.js
import { Scene } from 'phaser'
export default class BootScene extends Scene {
constructor () {
super({ key: 'BootScene' });
}
showMsg = null
create () {
this.scene.start('PlayScene')
}
}
PlayScene.js
import { Scene } from 'phaser'
export default class PlayScene extends Scene {
constructor () {
super({ key: 'PlayScene' })
}
create () {
// 添加文字
this.showMsg = this.add.text(16, 16, 'Play Scene!',
{ fontSize: '32px', fill: '#fff' });
this.input.on("pointerdown",function(){
this.scene.setVisible(false)
this.scene.start('OneScene')
this.scene.setVisible(true);
},this);
}
}
OneScene.js
import { Scene } from 'phaser'
export default class PlayScene extends Scene {
constructor () {
super({ key: 'OneScene' })
}
create () {
// 添加文字
this.showMsg = this.add.text(16, 16, 'One Scene!',
{ fontSize: '32px', fill: '#fff' });
this.input.on("pointerdown",function(){
this.scene.setVisible(false)
this.scene.start('TwoScene')
this.scene.setVisible(true);
},this);
}
}
TwoScene.js
import { Scene } from 'phaser'
export default class PlayScene extends Scene {
constructor () {
super({ key: 'TwoScene' })
}
create () {
// 添加文字
this.showMsg = this.add.text(16, 16, 'Two Scene!',
{ fontSize: '32px', fill: '#fff' });
this.input.on("pointerdown",function(){
this.showMsg.setVisible(false)
this.over = this.add.text(16, 16, 'Over!',
{ fontSize: '32px', fill: '#fff' });
},this);
}
}
本节完整代码下载地址
https://github.com/biwow/phaser3/tree/main/code/9