Phaser HTML5背景随特定分数而变化

时间:2015-02-18 04:29:43

标签: html5 phaser-framework

一旦玩家达到50分或任何数字,我试图改变移相器游戏中的背景,后面是png。这可能吗?

这是我的代码:

this.backgroundGame = this.game.add.tileSprite(0, 0, 800, 600, 'background');

     if (this.score == 3)
        this.backgroundGame = this.game.add.tileSprite(0, 0, 800, 600, 'background1');

但这肯定不起作用,如果我在更新函数中包含if,它会带来background1并覆盖整个游戏,如果我将它包含在create函数中,它就会被忽略。有什么建议吗?对不起,我是新手,还在学习。英语也不是我的第一语言,所以如果我的问题不明确请告诉我。

2 个答案:

答案 0 :(得分:1)

在create方法中,您必须同时设置两个(或多个)背景。正如Phaser中的层一样,CSS中的原始背景(第一个)必须是创建中的最后一个:

---创建:

this.background2 = this.game.add.tileSprite(0, 0, 800, 600, 'background1');
this.backgroundOriginal = this.game.add.tileSprite(0, 0, 800, 600, 'background');

然后在更新中,当您的条件为真时,您必须将alpha = 0(不可见)设置为原始背景,并且后面的图层将可见。

---更新:

if(this.score == 3) {
    this.backgroundOriginal.alpha = 0;
}
PD:我的英语也不够好... pero supongo queelinglésespañolizadosilo entiendes bien,¿no Rafa? ;)

答案 1 :(得分:1)

你可以在preload()方法中加载你需要的所有背景。

  this.game.load.image('background', 'images/bg1.png');
  this.game.load.image('background2', 'images/bg2.png');

稍后添加到游戏中的一个精灵像背景:

this.backgroundSprite = this.game.add.tileSprite(0, 0, 800, 600, 'background');
this.backgroundSprite = this.game.add.sprite(0, 0,'background');

何时才是正确的时间:

if (this.score == 3)
    this.backgroundSprite.loadTexture('background1');

当你不总是将新对象添加到游戏单词时,你将有更好的表现。