-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextFrame.as
More file actions
59 lines (51 loc) · 1.38 KB
/
Copy pathNextFrame.as
File metadata and controls
59 lines (51 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package iphstich.library
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.EventDispatcher;
/**
* Allows for an easy system to run a function at the beiggining of the next ENTER_FRAME dispatch.
* @author IphStich - johnathon.warren@yahoo.com.au
*/
public class NextFrame
{
//private static var dummy:MovieClip;
private static var initialized:Boolean = false;
private static var functions:Vector.<Function>;
private static var delays:Vector.<uint>;
public static function perform (fn:Function, delay:uint = 0):void
{
if (!initialized) throw new Error("NextFrame has not been initliazed. The init() functions needs to be run first.")
functions.push(fn)
delays.push(delay);
}
public static function init(stage:DisplayObject):void
{
if (initialized) return;
functions = new Vector.<Function>();
delays = new Vector.<uint>();
stage.addEventListener(Event.ENTER_FRAME, everyFrame, false, int.MAX_VALUE);
initialized = true;
}
private static function everyFrame(e:Event):void
{
var i:int = functions.length-1;
if (i > -1)
{
while (i >= 0)
{
if (delays[i] == 0) {
functions[i]();
functions.splice(i, 1);
delays.splice(i, 1);
} else {
--delays[i];
}
--i;
}
}
}
}
}