我有一个变量,我希望程序不断检查它的值。代码是这样的:
var score = 1
if (player touches the star)
{
player moves to bottom of screen
score = score + 1
}
if (score == 2)
{
a bunch of images.x = 1300
`your text`}
这显然不是真正的代码(我使用 javascript 和 Phaser 库),但我希望你明白
我尝试更改代码中 if (score == 2) { a lot images move off screen} 的位置,但它不起作用。我什至将图像 .x 属性提到的位置放在 if (score > 2) if 语句内,但无论如何它都没有移动图像。我认为解决这个问题的唯一方法是不断检查分数变量。
多次检查变量的最简单方法是 James 在游戏循环中提到的
update
函数。
var config = {
type: Phaser.AUTO,
width: 536,
height: 183,
scene: {
create,
update
},
// ...
};
function create () {
// ...
}
function update () {
//... this function is called 60 times per second
}
new Phaser.Game(config);
您只需要注意如何访问变量即可。使对象的一部分像这样
this.score = 2
或使全局变量。 我不会推荐后者。