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. 【Thrift一】Thrift安装部署

    Thrift安装部署 Thrift安装部署 下载源码包 安装g++ 解压Thrift安装包 安装boost开发工具 测试(python版) 下载源码包 wget http://apache.fayea ...

  2. 廖老师的Python教程——安装Python

    因为Python是跨平台的,它可以运行在Windows.Mac和各种Linux/Unix系统上.在Windows上写Python程序,放到Linux上也是能够运行的. 要开始学习Python编程,首先 ...

  3. Python的scrapy之爬取51job网站的职位

    今天老师讲解了Python中的爬虫框架--scrapy,然后带领我们做了一个小爬虫--爬取51job网的职位信息,并且保存到数据库中 用的是Python3.6  pycharm编辑器 爬虫主体: im ...

  4. 网络基础,tpc,udp

    一 , 网络基础相关知识 1. 架构 (重点) C / S  架构 : client 客户端(APP) 和 server 服务器端 能充分发挥pc机的性能 B / S 架构 : browser 浏览器 ...

  5. C语言实例解析精粹学习笔记——42(插入排序)

    实例说明: 将一个整数数组按从小到大的顺序进行排序.(主要学习基本的插入排序和改进的冒泡排序的算法和应用) 思路1: 从第一个数据开始,分别比较其后的数据,若比它小,则将这两个数的位置交换:从第一个数 ...

  6. Nodejs模块初始化

    模块初始化 一个模块中的JS代码仅在模块第一次被使用时执行一次,并在执行过程中初始化模块的导出对象.之后,缓存起来的导出对象被重复利用. 主模块 通过命令行参数传递给NodeJS以启动程序的模块被称为 ...

  7. luogu4172 [WC2006]水管局长

    就是用 lct 维护最小生成树 ref #include <algorithm> #include <iostream> #include <cstdio> #in ...

  8. 一次和别人争吵一个按钮,点击后显示导航;再点击不显示的效果,是否一定以及必须用js?

    事情经过是这样的,我们组一个说话很喜欢用一定,肯定的哥们,吃午饭的时候拿了自己做的一个UI库,头部有一个按钮 点击展开,再次点击收缩,他意思说一个按钮无法记录点击状态,必须使用js.然后我看了一眼,心 ...

  9. Linux中java应用程序的部署,使其开机自动启动

    初步需求:将在Windows/MyEclipse中开发的java应用程序部署到Linux服务器上,使其运行 针对需求,可以参考下面这些文章,但是这些文章很多东西没有提及到,我自己尝试部署运行 在lin ...

  10. localStorage简析

    声明:引用自http://www.cnblogs.com/st-leslie/p/5617130.html 一.什么是localStorage.sessionStorage 在HTML5中,新加入了一 ...