ActionQueue
Kind of class: | public class |
---|---|
Package: | |
Inherits from: |
|
Implements: | |
Classpath: | org.asaplibrary.util.actionqueue.ActionQueue |
File last modified: | Wednesday, 11 May 2011, 18:51:25 |
- Total rewrite of the code
- ActionQueue now runs generic Action objects (Command pattern)
- ActionQueue actions can be ActionQueues in itself
- Because of a (relatively small) syntax change, any call to a ready-made animation class like AQFade is now compile checked
- Integrated looping from ExtendedActionQueue (now defunct)
- Added markers and events from markers
- Added conditions
- Markers and conditions combined make it possible to synchronize separate queues
- Method "addInAction" has been replaced with "addAsynchronousAction"; asynchronous actions still run in their own thread, but can be paused, stopped and quit by the main queue
-
This ActionQueue moves a MovieClip, then lets it fade out to alpha 0 in .5 seconds:
my_mc.alpha = 0; var queue:ActionQueue = new ActionQueue(); var CURRENT:Number = Number.NaN; queue.addAction(new AQMove().move( my_mc, 1.0, CURRENT, CURRENT, 800, CURRENT, Cubic.easeOut )); queue.addAction(new AQFade().fade( my_mc, .5, CURRENT, 0 ));
-
ActionEvent with type:
STOPPED
-
ActionEvent with type:
PAUSED
-
ActionEvent with type:
RESUMED
-
ActionEvent with type:
QUIT
-
ActionEvent with type:
MARKER_VISITED
-
ActionEvent with type:
STARTED
-
ActionEvent with type:
FINISHED
Summary
-
addAction
(...args:Array) : void
- Creates an Action from input arguments and adds it to the queue.
-
addAsynchronousAction
(...args:Array) : void
- Adds an Action that is run asynchronously: other actions will follow right after and will not wait for this action to finish.
- getActions () : Array
- isRunning () : Boolean
-
stop
() : void
- Stops the queue.
-
addCondition
(inCondition:Condition) : void
- Adds a Condition to the queue.
-
addPause
(inDuration:Number = Number.NaN) : void
- Use pause() to add a paused state to the queue; when this action is run the queue is paused until resume is called on it.
-
togglePlay
() : void
- Toggles the playing/paused state of the queue.
-
pause
(inContinueWhereLeftOff:Boolean = true) : void
- Pauses the current running action and all asynchronous actions.
-
resume
() : void
- Resumes a paused queue.
-
quit
() : void
- Quits the queue.
-
skip
() : void
- Skips to the next action (if any).
-
goToMarker
(inMarkerName:String) : void
- Jumps the action pointer to a marker.
-
addGoToMarker
(inMarkerName:String) : void
- Adds a "go to marker" action to the queue.
-
addMarker
(inMarkerName:String) : void
- Adds a marker to the queue.
-
addStartLoop
(inLoopName:String, inLoopCount:int) : void
- Adds a "start of loop" marker to the queue.
-
addEndLoop
(inLoopName:String) : void
- Adds a "end of loop" marker to the queue.
-
endLoop
(inLoopName:String, inAbortLoop:Boolean = false) : void
- Ends a current running loop.
-
lastVisitedMarker
() : String
- Name of the last visited marker.
-
didVisitMarker
(inMarkerName:String) : Boolean
- Check whether the queue did pass a marker.
- isFinished () : Boolean
-
reset
() : void
- Resets the queue: stops current ActionRunners, removes all actions, clears the "visited markers" history.
- isPaused () : Boolean
-
run
() : *
- Starts the queue.
Instance methods
addAction
Creates an Action from input arguments and adds it to the queue.
This method accepts a number of argument configurations that are translated into an Action object. You can also directly add an Action or TimedAction object.
Adding an Action object
You can add any object of type Action, IAction, TimedAction and ITimedAction - including complete ActionQueues!
public function addAction (inAction:IAction) : void
Example:
var queue:ActionQueue = new ActionQueue(); var action:Action = new Action(createShapes, 10); queue.addAction(action); queue.run();Adding a local function Will be called in local scope.
public function addAction (inFunction:Function, argument1, argument2, ...) : void
Example:
var queue:ActionQueue = new ActionQueue(); queue.addAction(createShapes, 10); queue.run();Adding a object's method Will be called in the object's scope.
public function addAction (inMethod:Function, argument1, argument2, ...) : void
Example:
var queue:ActionQueue = new ActionQueue(); queue.addAction(mShapeCreator.createShapes, 10); queue.run();Adding a object's method by name Will be called in the object's scope. The method must have public access.
public function addAction (inMethodOwner:Object, inMethodName:String, argument1, argument2, ...) : void
Example:
var queue:ActionQueue = new ActionQueue(); queue.addAction(mShapeCreator, "createShapes", 10); queue.run();Using ready-made animation methods You can use any function that returns a TimedAction, or use one of the ready made class metods from these classes: AQAddMove AQBlink AQFade AQFollowMouse AQMove AQPulse AQFunction AQScale AQSet AQTimeline Example: This example will fade out a MovieClip from the current alpha to 0 in 0.5 seconds:
var queue:ActionQueue = new ActionQueue(); var CURRENT:Number = Number.NaN; queue.addAction(new AQFade().fade(my_mc, .5, CURRENT, 0)); queue.run();Custom timed actions Similar to "Adding an Action": add a TimedAction object. Example:
var action:TimedAction = new TimedAction(doMoveAndScale, duration, effect); queue.addAction(action);... where function
doMoveAndScale
performs the animation based on the percentage value it receives:
protected function doMoveAndScale (inValue:Number) : Boolean { var percentage:Number = 1-inValue; // end of animation: inValue == 0 tClip.x = NumberUtils.percentageValue(initX, mClipData.endx, percentage); tClip.y = NumberUtils.percentageValue(initY, mClipData.endy, percentage); tClip.scaleX = tClip.scaleY = NumberUtils.percentageValue(1, mClipData.endscale, percentage); return true; // if false the action will stop }
addAsynchronousAction
Adds an Action that is run asynchronously: other actions will follow right after and will not wait for this action to finish. Asynchronous actions can be paused and stopped with pause and stop.
addCondition
Adds a Condition to the queue. A running queue is automatically halted as long as the condition is not met. The condition will be checked on each frame update (using FramePulse events.
-
var q1:ActionQueue = new ActionQueue(); var q2:ActionQueue = new ActionQueue(); var q3:ActionQueue = new ActionQueue(); var condition:Function = function () : Boolean { return (q1.didVisitMarker("BAR1") && q2.didVisitMarker("BAR1") && q3.didVisitMarker("BAR1")); } var condition:Condition = new Condition (condition); var rightPos:Number = 800; var duration:Number = 4.0; var CURRENT:Number = Number.NaN; q1.addAction(new AQMove().move(my_mc1, duration1, CURRENT, CURRENT, rightPos, CURRENT)); q1.addMarker("BAR1"); q1.addCondition(condition1); // q1 will continue here as soon as the condition is met // ... (similar for other queues) q1.run(); q2.run(); q3.run();
- Conditions are run by ConditionManager.
addEndLoop
Adds a "end of loop" marker to the queue.
addGoToMarker
Adds a "go to marker" action to the queue.
addMarker
Adds a marker to the queue. Markers can be used to receive an event when the queue passes a marker, or to jump to a specific point in the queue.
-
This example shows how to listen for marker events:
var queue:ActionQueue = new ActionQueue(); var CURRENT:Number = Number.NaN; queue.addAction(new AQMove().move(my_mc, duration, CURRENT, CURRENT, marker1_mc.x, marker1_mc.y)); queue.addMarker("MARKER_1"); queue.addAction(new AQMove().move(my_mc, duration, CURRENT, CURRENT, marker2_mc.x, marker2_mc.y)); queue.addMarker("MARKER_2");
Add a listener:queue.addEventListener(ActionEvent._EVENT, onActionEvent); queue.run();
public function onActionEvent (e:ActionEvent) : void { switch (e.subtype) { case ActionEvent.MARKER_VISITED: handleMarkerPassed(e); break; } } public function handleMarkerPassed (e:ActionEvent) : void { // do something }
addPause
Use pause() to add a paused state to the queue; when this action is run the queue is paused until resume is called on it. Use pause(duration) to add a waiting action to the queue.
addStartLoop
Adds a "start of loop" marker to the queue.
-
This example shows how to set loop markers to run a section 3 times:
var queue:ActionQueue = new ActionQueue(); queue.addAction(new AQMove().move(loop_mc, duration, CURRENT, CURRENT, marker1_mc.x, marker1_mc.y)); queue.addStartLoop("MY_LOOP", 3); queue.addAction(new AQMove().move(loop_mc, duration, CURRENT, CURRENT, marker2_mc.x, marker2_mc.y)); queue.addAction(new AQMove().move(loop_mc, duration, CURRENT, CURRENT, marker3_mc.x, marker3_mc.y)); queue.addAction(new AQMove().move(loop_mc, duration, CURRENT, CURRENT, marker1_mc.x, marker1_mc.y)); queue.addEndLoop("MY_LOOP"); queue.run();
didVisitMarker
Check whether the queue did pass a marker. The marker has to be 'run' - any jumps over in-between markers will not mark it as 'passed'.
- True if the marker action with name was visited; otherwise false.
endLoop
Ends a current running loop.
getActions
- The list of queued actions (including already run actions).
goToMarker
Jumps the action pointer to a marker.
isFinished
- True when all Actions (including asynchronous actions) are finished.
isPaused
- True when the main runner is paused.
isRunning
- True if the main ActionRunner is (still) running; false if it is stopped or finished. A paused but running queue will return true.
lastVisitedMarker
Name of the last visited marker.
pause
Pauses the current running action and all asynchronous actions.
-
ActionEvent with type:
PAUSED
quit
Quits the queue.
-
ActionEvent with type:
QUIT
reset
Resets the queue: stops current ActionRunners, removes all actions, clears the "visited markers" history.
resume
Resumes a paused queue.
-
ActionEvent with type:
RESUMED
run
Starts the queue.
-
ActionEvent with type:
STARTED
skip
Skips to the next action (if any). If the queue is paused, skip will resume the queue. WIll also skip a paused state.
stop
Stops the queue.
- Stops all ActionRunners.
-
ActionEvent with type:
STOPPED
togglePlay
Toggles the playing/paused state of the queue.