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用法的更多相关文章

  1. C# Timer用法及实例详解

    C# Timer用法有哪些呢?我们在使用C# Timer时都会有自己的一些总结,那么这里向你介绍3种方法,希望对你了解和学习C# Timer使用的方法有所帮助. 关于C# Timer类  在C#里关于 ...

  2. C# Timer用法及实例讲解

    摘自:http://www.cnblogs.com/xcsn/archive/2013/05/10/3070485.html 1.C# Timer用法及实例详解 http://developer.51 ...

  3. Android Timer用法

    Android考虑到线程安全问题,不允许在线程中执行UI线程,在Android中,有一个类:android.os.Handler,这个可以实现各处线程间的消息传递.先看段代码,这个实例化了一个Hand ...

  4. Winform Timer用法,Invoke在Timer的事件中更新控件状态

    System.Timers.Timer可以定时执行方法,在指定的时间间隔之后执行事件. form窗体上放一个菜单,用于开始或者结束定时器Timer. 一个文本框,显示定时执行方法. public pa ...

  5. Threading.Timer用法

    protected System.Threading.Timer executeTimer;//定时器 private int interval;//定时器执行间隔周期 executeTimer = ...

  6. java之定时器任务Timer用法

    在项目开发中,经常会遇到需要实现一些定时操作的任务,写过很多遍了,然而每次写的时候,总是会对一些细节有所遗忘,后来想想可能是没有总结的缘故,所以今天小编就打算总结一下可能会被遗忘的小点: 1. pub ...

  7. C# 定时执行方法: System.Timers.Timer用法示例

    System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒        private void Form1_Load(ob ...

  8. 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别

    System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...

  9. System.Timers.Timer用法

    System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒 private void Form1_Load(object se ...

随机推荐

  1. 微信中h5页面用window.history.go(-1)返回上一页页面不会重新加载问题

    问题描述: 在实际开发中遇到这样一个问题,业务需求涉及到返回上一页问题,第一时间想到了window.history.go(-1)方法,这样做本身没有任何问题,但是在微信中,安卓手机还好返回上一页页面会 ...

  2. HDSF读写文件

    HDFS 读取文件 HDFS的文件读取原理,主要包括以下几个步骤: 1.首先调用FileSystem对象的open方法,其实获取的是一个DistributedFileSystem的   实例. 2.D ...

  3. DJANGO2.0 关联表的必填 ON_DELETE

    DJANGO2.0 关联表的必填 ON_DELETE 参数的含义 - BUXIANGHEJIU 的博客 - CSDN 博客 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...

  4. Drupal 网站漏洞修复以及网站安全防护加固方法

    drupal是目前网站系统使用较多一个开源PHP管理系统,架构使用的是php环境+mysql数据库的环境配置,drupal的代码开发较为严谨,安全性较高,但是再安全的网站系统,也会出现网站漏洞,dru ...

  5. 141. 环形链表 LeetCode报错:runtime error: member access within null pointer of type 'struct ListNode'

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  6. FPGA算法学习(1) -- Cordic(圆周系统之旋转模式)

    三角函数的计算是个复杂的主题,有计算机之前,人们通常通过查找三角函数表来计算任意角度的三角函数的值.这种表格在人们刚刚产生三角函数的概念的时候就已经有了,它们通常是通过从已知值(比如sin(π/2)= ...

  7. C++ vector的reserve和resize详解

    vector 的reserve增加了vector的capacity,但是它的size没有改变!而resize改变了vector的capacity同时也增加了它的size!原因如下:      rese ...

  8. Hadoop学习(四) FileSystem Shell命令详解

    FileSystem Shell中大多数命令都和unix命令相同,只是两者之间的解释不同,如果你对unix命令有基本的了解,那么对于FileSystem Shell的命令,你将会感到很亲切. appe ...

  9. springmvc 配置多视图(jsp,freemarker,HTML等)

    SpringMVC 的 Controller 可以返回各种各样的视图.比如 JSP, JSON, Velocity, FreeMarker, XML, PDF, Excel, 还有Html字符流 等等 ...

  10. 初步学习pg_control文件之八

    接前文  初步学习pg_control文件之七  继续 看:catalog_version_no 代码如下: static void WriteControlFile(void) { ... /* * ...