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

问题描述 投票:0回答:2

我想在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');
};
countdowntimer phaser-framework
2个回答
2
投票

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

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);

};


0
投票

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

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

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

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

PlayState.updateTimer = function() {
    console.log('updateTimer was called: ' + this.timeInSeconds);
    this.timeInSeconds--;
    // etc.
© www.soinside.com 2019 - 2024. All rights reserved.