Last active
August 29, 2015 13:56
-
-
Save lynxerzhang/9109336 to your computer and use it in GitHub Desktop.
ExecuteQueue in flash's Starling framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.utils.Dictionary; | |
import starling.animation.IAnimatable; | |
import starling.core.Starling; | |
/** | |
* 方法队列调用 | |
* | |
* @example | |
* var c:ExecuteQueue = new ExecuteQueue(); | |
* c.add(...); // add some function you will need handle once in every frame | |
* c.start(); // finally call the start to run the queue | |
*/ | |
public class ExecuteQueue implements IAnimatable | |
{ | |
public function ExecuteQueue() | |
{ | |
} | |
private var queue:Vector.<Function> = new <Function>[]; | |
/** | |
* 用于记录在执行方法队列过程中需要删除的方法引用 | |
*/ | |
private var delay:Vector.<Function> = new <Function>[]; | |
private var record:Dictionary = new Dictionary(); | |
private var index:int = 0; | |
private var queueLen:int = 0; | |
private var isExecute:Boolean = false; | |
/** | |
* 开始执行队列调用 | |
*/ | |
public function start():void | |
{ | |
Starling.current.juggler.add(this); | |
} | |
/** | |
* 停止队列调用 | |
*/ | |
public function stop():void | |
{ | |
Starling.current.juggler.remove(this); | |
} | |
/** | |
* 销毁 | |
*/ | |
public function dispose():void | |
{ | |
stop(); | |
delay = null; | |
queue = null; | |
record = null; | |
} | |
/** | |
* 清空 | |
*/ | |
public function purge():void | |
{ | |
stop(); | |
delay.length = 0; | |
queue.length = 0; | |
record = new Dictionary(); | |
loopIndex = 0; | |
isExecute = false; | |
queueLen = 0; | |
index = 0; | |
} | |
/** | |
* 添加一个方法引用 | |
* @param fun | |
*/ | |
public function add(fun:Function):void | |
{ | |
if (!(fun in record)) { | |
index = queue.length; | |
queue[index] = fun; | |
record[fun] = index; | |
queueLen = index + 1; | |
} | |
} | |
/** | |
* 移除一个方法引用 | |
* @param fun | |
*/ | |
public function remove(fun:Function):void | |
{ | |
if (fun in record) { | |
var delIndx:int = record[fun]; | |
if (isExecute) { | |
delay[delay.length] = queue[delIndx]; | |
} | |
else { | |
removeQueue(delIndx); | |
delete record[fun]; | |
} | |
} | |
} | |
private function removeQueue(idx:int):void | |
{ | |
var len:int = queue.length - 1; | |
queue[idx] = null; | |
if (len > 0) { | |
var fun:Function; | |
for (var i:int = idx; i < len; i ++) { | |
fun = queue[i] = queue[int(i + 1)]; | |
record[fun] = i; | |
} | |
} | |
queue.length = queueLen = len; | |
} | |
private var loopIndex:int = 0; | |
/* INTERFACE starling.animation.IAnimatable */ | |
public function advanceTime(time:Number):void | |
{ | |
if (queueLen > 0) { | |
isExecute = true; | |
var len:int = queueLen; | |
for (loopIndex = 0; loopIndex < len; loopIndex ++) { | |
queue[loopIndex](); | |
} | |
isExecute = false; | |
clearDelay(); | |
} | |
} | |
private function clearDelay():void | |
{ | |
var len:int = delay.length; | |
if (len > 0) { | |
for (var i:int = 0; i < len; i ++) { | |
remove(delay[i]); | |
} | |
delay.length = 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment