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/
Phaser的timer用法的更多相关文章
- C# Timer用法及实例详解
C# Timer用法有哪些呢?我们在使用C# Timer时都会有自己的一些总结,那么这里向你介绍3种方法,希望对你了解和学习C# Timer使用的方法有所帮助. 关于C# Timer类 在C#里关于 ...
- C# Timer用法及实例讲解
摘自:http://www.cnblogs.com/xcsn/archive/2013/05/10/3070485.html 1.C# Timer用法及实例详解 http://developer.51 ...
- Android Timer用法
Android考虑到线程安全问题,不允许在线程中执行UI线程,在Android中,有一个类:android.os.Handler,这个可以实现各处线程间的消息传递.先看段代码,这个实例化了一个Hand ...
- Winform Timer用法,Invoke在Timer的事件中更新控件状态
System.Timers.Timer可以定时执行方法,在指定的时间间隔之后执行事件. form窗体上放一个菜单,用于开始或者结束定时器Timer. 一个文本框,显示定时执行方法. public pa ...
- Threading.Timer用法
protected System.Threading.Timer executeTimer;//定时器 private int interval;//定时器执行间隔周期 executeTimer = ...
- java之定时器任务Timer用法
在项目开发中,经常会遇到需要实现一些定时操作的任务,写过很多遍了,然而每次写的时候,总是会对一些细节有所遗忘,后来想想可能是没有总结的缘故,所以今天小编就打算总结一下可能会被遗忘的小点: 1. pub ...
- C# 定时执行方法: System.Timers.Timer用法示例
System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒 private void Form1_Load(ob ...
- 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别
System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...
- System.Timers.Timer用法
System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒 private void Form1_Load(object se ...
随机推荐
- Mysql基础3-数据操作语言DML-数据查询语言DQL
主要: 数据操作语言DML 数据查询语言DQL 数据操作语言DML DML: Data Mutipulation Language 插入数据(增) 一般插入数据形式 1)形式1: insert [in ...
- phpcms2008网站漏洞如何修复 远程代码写入缓存漏洞利用
SINE安全公司在对phpcms2008网站代码进行安全检测与审计的时候发现该phpcms存在远程代码写入缓存文件的一个SQL注入漏洞,该phpcms漏洞危害较大,可以导致网站被黑,以及服务器遭受黑客 ...
- Python未彻底测试的项目
第一 socket 第二 twisted 第三 tornado 第四 微信网页版本登录 第五:进程,线程,协程间关系 第六:TCP三次握手 第七:堡垒机 第八:重写django admin
- svn 用cmd命令行启动服务
部署好svn 服务器后,用cmd命令行 svnserve -d -r [仓库地址] 启动服务,这样别的用户可以通过网络访问svn服务器了.
- Qt HUD(平显)演示程序绿色版
把一个黑底白字的程序改成黑底绿字 上对比图,左侧是原本,右侧是仿造,有些地方比例还是有问题 其实这个程序没有啥技术含量,就是画 #include "mainwindow.h" #i ...
- appium + Python + iOS 滑屏方法(appium版本大于1.5)
之前一直在搞android的自动化,滑动操作一直都用swipe(),比如: he1 = int(dr.get_window_size()['height'] * 0.8)he2 = int(dr.ge ...
- pytest 测试报告
测试报告 运行测试用例后,为了保存结果,我们需要生成测试报告,同时可以把运行的测试报告发送相关人员查阅,这时需要安装一个插件(pytest-html) pytest-html插件安装 pip inst ...
- 04-Mysql----初识sql语句
本节课先对mysql的基本语法初体验. 操作文件夹(库) 增 create database db1 charset utf8; 查 # 查看当前创建的数据库 show create database ...
- 分布式一致性算法之Paxos原理剖析
概述 Zookeeper集群中,只有一个节点是leader节点,其它节点都是follower节点(实际上还有observer节点,不参与选举投票,在这里我们先忽略,下同).所有更新操作,必须经过lea ...
- laravel 学习随笔(一)
1.路由参数:路由参数总是通过花括号进行包裹,参数在路由被执行时会被传递到路由的闭包.(路由参数不能包含“-”字符,如有需要可以用“_”代替):