【问题标题】:How to show the current time in Phaser framework?如何在 Phaser 框架中显示当前时间?
【发布时间】:2015-05-27 17:33:54
【问题描述】:

我似乎找不到如何在 Phaser 框架中显示当前时间的示例。我已经浏览了官方网站上的examples,我能想到的只是计时器倒计时的例子,或者朝某事倒计时(比如this example,例如,双关语:))。

【问题讨论】:

    标签: phaser-framework


    【解决方案1】:

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

    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

    【讨论】:

    • 谢谢,你是对的,我将你的答案标记为正确的。
    【解决方案2】:

    由于我确实没有找到官方示例,所以我把它放在这里,仅供参考,以便它可以帮助那些将搜索与我相同的确切查询词的人。

    似乎比我想象的要容易。简单地说,使用 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;
    }
    

    【讨论】:

    • 这不会导致任何性能限制,因为它会不断被调用(大约每分钟 3600 次)。
    猜你喜欢
    • 1970-01-01
    • 2022-08-20
    • 2012-06-17
    • 1970-01-01
    • 2023-03-27
    • 2011-05-21
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多