如何在Phaser框架中显示当前时间?

时间:2015-05-26 18:58:37

标签: phaser-framework

我似乎找不到如何在Phaser框架中显示当前时间的示例。我已经通过了官方网站上的examples,而我所能提出的只是计时器倒计时的例子,或者是某些东西(例如this example),例如,双关语意图:))。

2 个答案:

答案 0 :(得分:3)

您的示例将有效。但是,它会检查每次更新的时间(每帧运行一次),这在计算上更加昂贵并且可能会减慢您的游戏速度。这是一个带有计时器的解决方案,可以每秒更新一次:

var timeString;
var timeText;

function create() {
    var style = { fill : "#FFFFFF" };
    timeText = game.add.text(200, 200, timeString, style);

    var timer = game.time.create();
    timer.repeat(1 * Phaser.Timer.SECOND, 7200, updateTime, this);
    timer.start();
}

function updateTime() {
    var time = new Date();

    var hours = time.getHours();
    var minutes = time.getMinutes();
    var seconds = time.getSeconds();

    if (hours < 10) {
        hours = "0" + hours;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }

    timeString = hours + ":" + minutes + ":" + seconds;
    timeText.text = timeString;
}

Link to Phaser Sandbox with this example

答案 1 :(得分:1)

由于我真的没有找到一个正式的例子,我把它放在这里,只是为了进一步参考,所以它可以帮助那些将会像我一样搜索相同的查询词的人

它似乎比我想象的要容易。简单地说,使用JavaScript函数更新update方法中的文本,该函数使用Date对象返回所需的时间信息:

var timeText = game.add.text(10, 10, "00:00:00");
function update(){
    timeText.setText(getCurrentTime());
}

function getCurrentTime(){
    var currentdate = new Date(); 
    var time = currentdate.getHours() + ":"  + currentdate.getMinutes() + ":" + currentdate.getSeconds();
    return time;
}