zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    8086 CPU 寄存器
    python中 * 的用法
    字典的相应操作
    tesseract学习记录
    C学习之路2012.8.28
    函数库管理
    2013.3.19C++浏览记录。。。
    自动生成makefile文件学习
    整理做过的东西(电子警察)
    基于zed的tesseract移植过程记录
  • 原文地址:https://www.cnblogs.com/mengff/p/9486191.html
Copyright © 2011-2022 走看看