- 注册时间
- 2013-2-20
- 最后登录
- 2020-1-26
- 阅读权限
- 45
- 积分
- 1866
- 精华
- 1
- 帖子
- 181
|
lufy 发表于 2015-8-7 20:05
Re可能是写的时候不小心多了个e,不过这个倒没什么影响,因为实际用的时候,不需要关心它的值是什么,
这 ...
我看了下前辈的LTimer代码,感觉写得太复杂了,我自己也写了个代码稍微短点的版本(遵照Adobe提供的as 3.0语法),放到这里,还望lufy考虑改进一下- LGlobal.timerList = new Array();
- LGlobal.onShow = function () {
- if (LGlobal.canvas == null) {
- return;
- }
- if(LGlobal._outStageCheckCount <= 0){
- LGlobal._outStageCheckCount--;
- if(LGlobal._outStageCheckCount < -2){
- LGlobal.stage.dispatchEvent(new LEvent(LFocusEvent.FOCUS_OUT));
- LGlobal._outStageCheckCount = 1;
- }
- }
- if (LGlobal.fpsStatus) {
- LGlobal.fpsStatus.reset();
- }
- if (LGlobal.timerList) {
- LGlobal.updateTimers();
- }
- if (LGlobal.stage.onresizeEvent) {
- LGlobal.stage.onresizeListener(LGlobal.stage.onresizeEvent);
- delete LGlobal.stage.onresizeEvent;
- }
- if (LGlobal.forceRefresh) {
- LGlobal.canvasObj.width = LGlobal.canvasObj.width;
- LGlobal.forceRefresh = false;
- }
- LGlobal.canvas.beginPath();
- if (LGlobal.box2d != null) {
- LGlobal.box2d.ll_show();
- if (!LGlobal.traceDebug && LGlobal.keepClear) {
- LGlobal.canvas.clearRect(0, 0, LGlobal.width + 1, LGlobal.height + 1);
- }
- } else {
- if (LGlobal.keepClear) {
- LGlobal.canvas.clearRect(0, 0, LGlobal.width + 1, LGlobal.height + 1);
- }
- if (LGlobal.backgroundColor !== null) {
- LGlobal.canvas.fillStyle = LGlobal.backgroundColor;
- LGlobal.canvas.fillRect(0, 0, LGlobal.width, LGlobal.height);
- }
- }
- LGlobal.show(LGlobal.childList);
- };
- LGlobal.updateTimers = function () {
- var list = LGlobal.timerList, l = list.length;
- if (list.length == 0) {
- return;
- }
- for (var i = 0; i < l; i++) {
- var timer = list[i];
- timer.ll_update();
- }
- };
- var LTimer = (function () {
- function LTimer (delay, repeatCount) {
- var s = this;
- LExtends(s, LEventDispatcher, []);
- s.type = "LTimer";
- s.delay = delay || 0;
- s.repeatCount = repeatCount || 0;
- s.currentCount = 0;
- s.running = false;
- s.ll_indexInList = null;
- s.ll_previousTime = null;
- }
- var p = {
- start : function () {
- var s = this;
- if (s.running) {
- return;
- }
- s.running = true;
- s.ll_previousTime = (new Date()).getTime();
- s.ll_indexInList = LGlobal.timerList.length;
- LGlobal.timerList.push(s);
- },
- stop : function () {
- var s = this;
- if (!s.running) {
- return;
- }
- s.running = false;
- LGlobal.timerList.splice(s.ll_indexInList, 1);
- s.ll_indexInList = null;
- },
- reset : function () {
- var s = this;
- s.currentCount = 0;
- s.ll_previousTime = null;
- s.stop();
- },
- ll_update : function () {
- var s = this, currentTime = (new Date()).getTime();
- if (currentTime - s.ll_previousTime >= s.delay) {
- s.ll_previousTime = currentTime;
- s.currentCount++;
- s.dispatchEvent(LTimerEvent.TIMER);
- if (s.currentCount == s.repeatCount) {
- s.stop();
- s.dispatchEvent(LTimerEvent.TIMER_COMPLETE);
- }
- }
- }
- };
- for (var k in p) {
- LTimer.prototype[k] = p[k];
- }
- return LTimer;
- })();
复制代码- var LTimerEvent = function () {throw "LTimerEvent cannot be instantiated";};
- LTimerEvent.TIMER = "timer";
- LTimerEvent.TIMER_COMPLETE = "timerComplete";
复制代码 就是有个疑问,我看文档上写的repeatCount默认是0,而您写的代码是无穷大,不知道到底哪个才是对的。 |
|