Phaser的timer用法

1. 延迟timer,相当于setTimeout

game.time.events.add(Phaser.Timer.SECOND*5,this.delayOver,this);

2. 循环timer,相当于setInterval

game.time.events.loop(Phaser.Timer.SECOND,this.addMonster,this);

3. 停止一个timer

this.monsterTimer = game.time.events.loop(Phaser.Timer.SECOND,this.addMonster,this);
game.time.events.remove(this.monsterTimer);

4. Phaser中的时间常量

Phaser.Timer.SECOND =1 second
Phaser.Timer.SECOND*5 =5 seconds
Phaser.Timer.SECOND/2= half a second or call the function twice a second
Phaser.Timer.SECOND/10 =one tenth a second

5. 创建一个倒计时的例子

复制代码
var StateMain = {
    preload: function() {},
    create: function() {
        //total time until trigger
        this.timeInSeconds = 120;
        //make a text field
        this.timeText = game.add.text(game.world.centerX, game.world.centerY, "0:00");
        //turn the text white
        this.timeText.fill = "#ffffff";
        //center the text
        this.timeText.anchor.set(0.5, 0.5);
        //set up a loop timer
        this.timer = game.time.events.loop(Phaser.Timer.SECOND, this.tick, this);
    },
    tick: function() {
        //subtract a second
        this.timeInSeconds--;
        //find how many complete minutes are left
        var minutes = Math.floor(this.timeInSeconds / 60);
        //find the number of seconds left
        //not counting the minutes
        var seconds = this.timeInSeconds - (minutes * 60);
        //make a string showing the time
        var timeString = this.addZeros(minutes) + ":" + this.addZeros(seconds);
        //display the string in the text field
        this.timeText.text = timeString;
        //check if the time is up
        if (this.timeInSeconds == 0) {
            //remove the timer from the game
            game.time.events.remove(this.timer);
            //call your game over or other code here!
            this.timeText.text="Game Over";
        }
    },
    /**
     * add leading zeros to any number less than 10
     * for example turn 1 to 01
     */
    addZeros: function(num) {
        if (num < 10) {
            num = "0" + num;
        }
        return num;
    },
    update: function() {}
}
复制代码

 

出处:https://phasergames.com/phaser-timer-basics-tutorial/

posted @   全玉  阅读(641)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-08-16 ES6 import export
2016-08-16 gitingore配置样例
点击右上角即可分享
微信分享提示