如何在 Phaser 游戏中添加倒数计时器

How to add countdown timer to Phaser game

我正在尝试向 Phaser 游戏添加一个简单的倒数计时器。当计时器结束时,我希望游戏重新开始。添加相关代码后,控制台中没有错误,但我无法让计时器出现在屏幕上。我是 Phaser 的新手,仍在学习 Javascript。请问我哪里出错了?我只在下面发布了相关代码,以及用于游戏中已经存在的文本的代码运行良好(计算收集的硬币的文本)。

PlayState = {};

PlayState.init = function () {
//....
};

PlayState.preload = function () {

this.game.load.image('font:numbers', 'images/numbers.png'); //for the 
//HUD coin count - not the timer

};

PlayState.create = function () {

//TIMER CODE:
this.timeInSeconds = 120;
this.timeText = this.game.add.text(this.game.world.centerX, 
this.game.world.centerY, "0:00",{font: '15px Arial', fill: '#FFFFFF', align: 
'center'});
this.timeText.anchor.set(0.5, 0.5);
this.timer = this.game.time.events.loop(Phaser.Timer.SECOND, 
this.updateTimer, this);

};

PlayState.update = function () {

this.coinFont.text = `x${this.coinPickupCount}`; //for HUD coin count not 
//the timer

};

//TIMER CODE:
PlayState.updateTimer = function() {
    this.timeInSeconds--;
    var minutes = Math.floor(this.timeInSeconds / 60);
    var seconds = this.timeInSeconds - (minutes * 60);
    var timeString = this.addZeros(minutes) + ":" + this.addZeros(seconds);
    this.timeText.text = timeString;

    if (this.timeInSeconds == 0) {
        this.game.state.restart();
    }
};

 //add leading zeros to any number less than 10
 //for example turn 1 to 01

PlayState.addZeros = function(num) {
    if (num < 10) {
        num = "0" + num;
    }
    return num;
};

//BELOW IS CODE FOR THE COIN COUNT NOT THE TIMER
 PlayState._createHud = function () {

                this.keyIcon = this.game.make.image(0, 30, 'icon:key');
                this.keyIcon.anchor.set(0, 0.5);
                 const NUMBERS_STR = '0123456789X ';


                 this.coinFont = this.game.add.retroFont('font:numbers', 20, 
                 26,NUMBERS_STR, 6);

                let coinIcon = this.game.make.image(this.keyIcon.width + 7, 
                 0, 'icon:coin');

                let coinScoreImg = this.game.make.image(coinIcon.x + 
                coinIcon.width, coinIcon.height / 2, this.coinFont);
                coinScoreImg.anchor.set(0, 0.5);

                this.hud = this.game.add.group();
                this.hud.add(coinIcon);
                this.hud.position.set(10, 10);

                this.hud.add(coinScoreImg);
                this.hud.add(this.keyIcon);


                this.hud.fixedToCamera = true;

            };


window.onload = function () {
  let game = new Phaser.Game(1280, 800, Phaser.CANVAS, 'game');
  game.state.add('play', PlayState);
  game.state.start('play');
};

您将初始文本设置为“0:00”,即使这样也没有显示在屏幕上?我要做的第一件事是查看文本所在的坐标,也许它在屏幕外不可见。而不是 this.game.world.centerX, this.game.world.centerY 尝试像 100,100 这样的东西,它会出现吗?还要尝试设置非常长的初始文本,因此 "blah test ABC 123" 而不是“0:00” 可能会有所不同。

其次,Arial 字体可能由于某种原因无法使用。如果您省略 {font: '15px..'center'} 部分,它将使用默认字体,这会改变什么吗?

第三,你说你没有 post 这里的所有代码,但也许你不小心覆盖了代码中某处的变量 this.timeText ?因此,请检查您是否没有将该变量设置为其他变量。

最后,我会在 updateTimer 函数中添加一个 console.log,只是为了查看它是否被调用。所以:

PlayState.updateTimer = function() {
    console.log('updateTimer was called: ' + this.timeInSeconds);
    this.timeInSeconds--;
    // etc.

我终于解决了这个问题。文本没有显示,因为它是在创建背景图像之后呈现的。所以它在那里,但被背景图像隐藏了。我只是将定时器代码移到了创建的末尾,它现在可以工作了。

PlayState.create = function () {


  this.game.world.setBounds(0, 0, 2560, 800);

  background1 = this.game.add.sprite(0, 0, 'background');

  background2 = this.game.add.sprite(1280, 0, 'background2');

  this.game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  this.game.scale.setMinMax(480,320,1280,800);

  this.game.scale.windowConstraints.bottom = 'visual';

  this.game.add.image(0, 0, 'background');
  this._loadLevel(this.game.cache.getJSON('level:1'));

 this._createHud();

//TIMER CODE SHOULD GO HERE AND NOT AT THE BEGINNING OF CREATE

    this.timeInSeconds = 120;
    this.timeText = this.game.add.text(220, 30, "0:00",{font: '30px Arial', fill: 
    '#FFFFFF', align: 'center'});
    this.timeText.anchor.set(0.5, 0.5);
    this.timer = this.game.time.events.loop(Phaser.Timer.SECOND, this.updateTimer, 
    this);

};