Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

Sequence.as

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
 * Sequence
 * creates a sequence of method calls that are called in sequence based 
 * upon completion events.
 */
package com.senocular.events {
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class Sequence extends EventDispatcher {
		
		private var calls:Array = new Array();	// list of SequenceEvent objects
		private var position:int = 0;			// position within calls
		private var currSeqEvt:SequenceEvent;	// current event
		private var nextSeqEvt:SequenceEvent;	// next event to be called when current event completes
		
		/**
		 * Constructor 
		 */
		public function Sequence() {}
		
		/**
		 * play
		 * initiates a sequence using current position (not necessarily 0)
		 */
		public function play():void {
			nextSeqEvt = calls[position];
			nextCall();
		}
		
		/**
		 * stop
		 * stops a sequence from continuing
		 */
		public function stop():void {
			
			// remove listener if currSeqEvt exists
			if (currSeqEvt) {
				currSeqEvt.target.removeEventListener(currSeqEvt.eventID, nextCall);
			}
		}
		
		/**
		 * reset
		 * reset a sequence stopping it and setting its position to 0
		 */
		public function reset():void {
			
			// remove listener if currSeqEvt exists
			if (currSeqEvt) {
				currSeqEvt.target.removeEventListener(currSeqEvt.eventID, nextCall);
			}
			
			// clear events and set position to 0
			currSeqEvt = null;
			nextSeqEvt = null;
			position = 0;
		}
		
		/**
		 * clear
		 * resets sequence and removes all calls 
		 */
		public function clear():void {
			reset();
			calls.length = 0;
		}
		
		/**
		 * addEvent
		 * adds a call to the sequence
		 */
		public function addEvent(callback:Function, args:Array = null, target:EventDispatcher = null, eventID:String = null):void {
			
			// create new SequenceEvent object to hold call, its arguments and event information
			var addedSeqEvt:SequenceEvent = new SequenceEvent(callback, args, target, eventID);
			
			// add event to calls
			calls.push(addedSeqEvt);
			
			// if added in the middle of a sequence and at the
			// end of that sequence, make this event the next event
			if (currSeqEvt && !nextSeqEvt) {
				nextSeqEvt = addedSeqEvt;
			}
		}
		
		
		/**
		 * nextCall - event handler
		 * method that calls the next call in the sequence
		 */
		private function nextCall(evt:Event = null):void {
			
			// if a call exists for the next event
			if (nextSeqEvt) {
				
				// save the next call to call it later
				var firing:SequenceEvent = nextSeqEvt;
				
				// remove listener if currSeqEvt exists
				if (currSeqEvt) {
					currSeqEvt.target.removeEventListener(currSeqEvt.eventID, nextCall);
				}
				
				// reassign current call to next call
				currSeqEvt = nextSeqEvt;
				
				// update position, if additional calls exist
				// reassign nextSeqEvt to the next call
				position++;
				if (position < calls.length) {
					nextSeqEvt = calls[position];
					currSeqEvt.target.addEventListener(currSeqEvt.eventID, nextCall);
				}else{
					
					// set next to null if no more calls exist
					nextSeqEvt = null;
				}
				
				// call the firing call
				firing.callback.apply(firing.target, firing.args);
				
				// if the firing target is itself, dispatch the default event
				// which will fire the next call in the sequence
				if (firing.target == firing) {
					firing.dispatchEvent(new Event(SequenceEvent.EVENT));
				}
			}else{
				
				// when complete, reset
                dispatchEvent(new Event(Event.COMPLETE)); // as requested by boon
				reset();
			}
		}
	}
}

import flash.events.Event;
import flash.events.EventDispatcher;

/**
 * SequenceEvent
 * object representing the callback/event information for an item the sequence
 */
class SequenceEvent extends EventDispatcher {
	
	public var target:EventDispatcher;		// object handling events for event id
	public var callback:Function;			// callback function in sequence
	public var args:Array = new Array();	// optional arguments for callback
	public var eventID:String;			// event id to indicate callback completion and start of next event
			
	public static const EVENT:String = "sequence";	// default event - fires directly after previous callback call
		
	/**
	 * Constructor 
	 */
	public function SequenceEvent(callback:Function, args:Array = null, target:EventDispatcher = null, eventID:String = null):void {
		
		// assign variables
		this.callback = callback;
		this.args = (args) ? args : new Array();
		
		// if target is passed, use it and COMPLETE for default event if not given
		if (target) {
			this.target = target;
			this.eventID = (eventID) ? eventID : Event.COMPLETE;
		}else{
			
			// default to this as event dispatcher using default sequence event 
			this.target = this;
			this.eventID = EVENT;
		}
	}
}