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/

  • 相关阅读:
    史上最全的浏览器 CSS & JS Hack 手册
    JavaScript1.6数组新特性和JQuery的几个工具方法
    用jquery循环map
    javascript强大的日期函数
    用 javascript 判断 IE 版本号
    常见排序算法基于JS的实现
    JavaScript中callee,caller,argument的理解
    apply()方法和call()方法
    虽然我们可能不想对元素应用3D变换,可我们一样可以开启3D引擎
    在移动端上加上代码,让字体变得平滑
  • 原文地址:https://www.cnblogs.com/mengff/p/9486191.html
Copyright © 2011-2022 走看看