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 ...
随机推荐
- ODBC error in PHP: “No tuples available at this result index”
ODBC error in PHP: “No tuples available at this result index” 在执行存储过程的时候发生如题的错误,在stackoverflow上找到了相同 ...
- memcached搭建
MemCache 安装使用 安装memcached之前首先需要安装libevent, 如果没有安装的请自行去安装. 下载memcache http://www.memcached.org/files/ ...
- 1. tty终端接收数据原理
1. 串口接收数据原理(以“ls”命令为例) (1) 每个终端设备都有一个接收缓冲区,保存原始数据.shell程序一直在休眠,等待合适的字符: (2) 串口接收到'l'字符,把它保存下来: (3) 串 ...
- python基础之初识
一. 计算机是什么 基本组成: 主板+cpu+内存 cpu: 主频, 核数(16) 内存:大小(8G, 16G, 32G) 型号: DDR3, DDR4, DDR5, 主频(海盗船,玩家国度) 显卡: ...
- Hash学习笔记
啊啊啊啊,这篇博客估计是我最早的边写边学的博客了,先忌一忌. 本文章借鉴与一本通提高篇,但因为是个人的学习笔记,因此写上原创. 目录 谁TM边写边学还写这玩意? 后面又加了 Hash Hash表 更多 ...
- 如何在WIN7_64环境下安装Oracle10g_64位版本
转载请注明出处http://www.cnblogs.com/SharpL/p/4338638.html 1.如果之前安装过Oracle软件,建议完全卸载(究竟有没有必要_不知道_我是这么做的) 2.清 ...
- uber司机已经激活了,就是还没有上传头
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- oracle 11g XE 学习版添加scott用户方法全步骤
安装企业版的orcale是不是太费时费力了?若只是学习用途的话,不妨试试轻便版的XE版本,同样是官网下载的,但是这个安装起来比完整版简便多了. 首先,你得先安装好orcale 11g XE 版本:(这 ...
- 2007: [Noi2010]海拔
2007: [Noi2010]海拔 https://www.lydsy.com/JudgeOnline/problem.php?id=2007 分析: 平面图最小割. S在左下,T在右上,从S到T的一 ...
- LeetCode:21. Merge Two Sorted Lists(Easy)
1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...