码迷,mamicode.com
首页 > 其他好文 > 详细

Phaser的timer用法

时间:2018-08-16 13:31:18      阅读:935      评论:0      收藏:0      [点我收藏+]

标签:HERE   from   add   seconds   complete   center   state   splay   set   

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/

Phaser的timer用法

标签:HERE   from   add   seconds   complete   center   state   splay   set   

原文地址:https://www.cnblogs.com/mengff/p/9486191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!