【发布时间】:2019-07-30 03:47:36
【问题描述】:
我正在创建一种“太空侵略者”类游戏,但在将分数从 WorldScene 传递到 GameOverScene 时确实遇到了问题。
我已经尝试使用 init: function() 来传递数据,但我可能做错了什么......
我正在使用 Phaser 3。
我的 WorldScene 的一点点:
constructor() {
super( { key: 'WorldScene'} );
}
preload()
{
}
create()
{
var score = 0;
(...)
//a scene comeca de novo
//this.scene.restart();
this.scene.start('GameOverScene', {score_n: this.score} );
}, [], this);
还有我在 game.js 中的 GameOverScene
Extends: Phaser.Scene,
initialize:
function GameOverScene ()
{
Phaser.Scene.call(this, { key: 'GameOverScene' });
},
init: function (data)
{
console.log('init', data);
this.scoreFinal = data.score_n;
},
preload: function ()
{
//background inicial
this.load.image('back_menu', 'assets/img_espaco.png');
},
create: function (data)
{
let backg = this.add.sprite(0, 0, 'back_menu');
backg.setOrigin(0, 0);
//texto do game over
gameOver_text = this.add.text(320, 150, 'Game Over', { fontSize: '70px', fill: '#FF0000' });
gameOver_text.setOrigin(0.5);
//score
scoreFinal_txt = this.add.text(320, 210, 'Pontuação Final: '+ this.scoreFinal, { fontSize: '20px', fill: '#FFF' });
scoreFinal_txt.setOrigin(0.5);
//botao de recomeçar
bt_novo = this.add.text(320, 260, 'Voltar ao Inicio', { fontSize: '30px', color: '#ffffff' }).setInteractive();
bt_novo.setOrigin(0.5);
//se o botao for carregado começa o jogo
bt_novo.on('pointerdown', function (event) { this.scene.start('MenuScene'); }, this );
bt_novo.on('pointerover', function (event) { bt_novo.setStyle({ fill: '#ff0'}); } )
bt_novo.on('pointerout', function (event) { bt_novo.setStyle({ fill: '#ffffff'}); } )
},
}); ```
My scoreFinal_txt shows: "Pontuaçao Final: undefined"
【问题讨论】:
-
我的回答对你有用吗?
标签: javascript html phaser-framework