速发国际365的最新网站-百特365平台可靠吗-日博365投注网

时间控制

时间控制1.Timer 计时器​假设有这样一个需求:游戏中设计一个倒计时功能,要求玩家在30秒内进行游戏操作,并达到一定要求方可过关。 在这

时间控制

时间控制1.Timer 计时器​假设有这样一个需求:游戏中设计一个倒计时功能,要求玩家在30秒内进行游戏操作,并达到一定要求方可过关。

在这个需求中,需要一个倒计时功能,在Egret中提供了计时器 Timer 来实现类似功能。

Timer 相关的有两个属性,三个方法和两个事件。

两个属性是 delay 与 repeatCount ,分别表示每次间隔的时间(以毫秒为单位)和执行的次数(如果次数为0,则表示不停的执行)。

三个方法为 start, reset 和 stop。作用分别是开始计时,重新计时和暂停计时。

两个事件分别为 TimerEvent.TIMER 和 TimerEvent.TIMER_COMPLETE 。分别在计时过程中触发和计时结束后触发。

示例演示:

class TimerDemo extends egret.DisplayObjectContainer { public constructor() { super(); //创建一个计时器对象 const timer: egret.Timer = new egret.Timer(500, 5); //注册事件侦听器 timer.addEventListener(egret.TimerEvent.TIMER, this.timerFunc, this); timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, this.timerComFunc, this); //开始计时 timer.start(); } private timerFunc() { console.log("计时"); } private timerComFunc() { console.log("计时结束"); }}编译后运行,效果如图:

2.Ticker 心跳​startTick(停止对应stopTick)全局函数将以 60 帧速率回调函数。它与 ENTER_FRAME 事件不同。ENTER_FRAME 是每帧回调,改变帧率会改变回调速度;startTick是定时回调,改变帧率也不会影响回调速度。

示例代码:

class startTickerTest extends egret.DisplayObjectContainer { public constructor() { super(); this.once(egret.Event.ADDED_TO_STAGE, this.onLoad, this); } private star: egret.Bitmap; private speed: number = 0.05; private time: number = 0; private onLoad(event: egret.Event) { const star: egret.Bitmap = new egret.Bitmap(RES.getRes("star")); this.addChild(star); this.star = star; this.time = egret.getTimer(); egret.startTick(this.moveStar, this); } private moveStar(timeStamp: number): boolean { const now = timeStamp; const time = this.time; const pass = now - time; console.log("moveStar: ", (1000 / pass).toFixed(5)); this.time = now; return false; }}

startTick 函数有两个传入参数,第一个参数是回调函数,该回调函数要求有返回值,如果返回为true将在回调函数执行完成之后立即重绘,为false则不会重绘。第二个参数是this对象,通常传入this即可。

下面修改回调函数中的代码:

private function moveStar(timeStamp: number): boolean { const now = timeStamp; const time = this.time; const pass = now - time; console.log("moveStar: ", (1000 / pass).toFixed(5)); this.star.x += this.speed * pass; if (this.star.x \> 300) egret.stopTick(this.moveStar, this); this.time = now; return false;}会得到类似上面的效果。startTick 函数的回调函数会传入一个参数为执行到当前所逝去的时间。

另外需要说明,上述代码中的全局函数 getTimer() 可以获取全局的 Egret 框架启动时间毫秒数。

3.帧事件​帧事件 ENTER_FRAME 在下一帧开始时回调。所以它的回调速率是跟帧率相关的。下面代码测试了不同帧率下的表现:

class startTickerTest extends egret.DisplayObjectContainer { public constructor() { super(); this.once(egret.Event.ADDED_TO_STAGE, this.onLoad, this); } private timeOnEnterFrame: number = 0; private onLoad(event: egret.Event) { this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this); this.timeOnEnterFrame = egret.getTimer(); } private onEnterFrame(e: egret.Event) { const now = egret.getTimer(); const time = this.timeOnEnterFrame; const pass = now - time; console.log("onEnterFrame: ", (1000 / pass).toFixed(5)); this.timeOnEnterFrame = egret.getTimer(); }}修改不同的帧率,结果不同:

在 ENTER_FRAME 的回调函数中修改显示对象的参数,即可完成简单的动画效果。

首先准备并在资源管理中配置好下面的素材:

示例代码:

star: egret.Bitmap;speed: number = 0.05;timeOnEnterFrame = 0;private function onLoad(event: egret.Event) { const star: egret.Bitmap = new egret.Bitmap(RES.getRes("star")); this.addChild(star); this.star = star; this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this); this.timeOnEnterFrame = egret.getTimer();}private function onEnterFrame(e: egret.Event) { const now = egret.getTimer(); const time = this.timeOnEnterFrame; const pass = now - time; //console.log("onEnterFrame: ", (1000 / pass).toFixed(5),pass); this.star.x += this.speed * pass; this.timeOnEnterFrame = egret.getTimer(); if (this.star.x \> 300) this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);}通过计算时间间隔来实现位移会使动画看起来更平滑一些,因为每帧的时间间隔不是固定的。

← 上一篇: 防震减灾知识科普丨应急避震有方法
下一篇: 苹果将推出低价版iPhone 5C →

相关推荐

綖的解释

綖的解释

【未集中】【糸】 綖·康熙筆画:13 ·部外筆画:7 【廣韻】以然切【集韻】【韻會】【正韻】夷然切,𠀤音延。【玉篇】冕前後垂覆也。【左

中国VS中国香港比赛分析

中国VS中国香港比赛分析

2025年7月15日,备受瞩目的四强赛将在中国与中国香港两支实力强劲的队伍之间展开较量。此次比赛不仅关系到双方晋级决赛的命运,更是两地足

热门手机排行

热门手机排行

您所在的位置:排行榜首页 > 手机排行榜 > 热门手机排行 手机排行榜 综合排行 手机综合排行榜 手机热门排行榜 手机品牌排行榜 手机飙升排行

电脑与MP3如何链接?怎样传输音乐文件?

电脑与MP3如何链接?怎样传输音乐文件?

电脑与MP3链接以及传输音乐文件通常比较简单,以下是具体的步骤: 链接电脑与MP3 1. 准备USB数据线:通常MP3播放器会附带一条USB数据线,用于

女性也能玩转电商直播运营!

女性也能玩转电商直播运营!

📢📢📢电商直播运营适合女性吗?这个问题的答案是肯定的!越来越多的女性在电商直播运营领域中展现出了非凡的才华和能力,成为了行业中

《中国有嘻哈》歌曲全收录!

《中国有嘻哈》歌曲全收录!

服务条款| 隐私政策| 儿童隐私政策| 版权投诉| 投资者关系| 广告合作 | 联系我们 廉正举报 不良信息举报邮箱: 51jubao@service.netease.com 互联网宗教