Nov 07, 2008

Simple Sequencer

We often have the problem of sequencing animations and events, e.g. 1. close all windows, 2. move somewhere else, 3. open windows.

To make our lives a little bit easier I was looking for a simple class that we can use to sequence those tasks. I looked around but couldn't find anything that was small, simple to fit our needs. So I wrote one called SimpleSequencer.



The class is not supadupa cleaned up but it works great and I've used it heavily in some of our recent projects.

This is how it works:
Create a new SimpleSequencer Instance. (The String you pass in the constructor is only used as for debugging at the moment.)

var sequence:SimpleSequencer = new SimpleSequencer('id');

Add a Listener to Event.COMPLETE.
sequence.addEventListener(Event.COMPLETE, _onComplete_handler);

Add steps to your sequence using the addStep function.
addStep($stepId:Number, $target:EventDispatcher, $functionToCallToStart:Function, $eventToListen:String, $args:Object=null);

The order in that you add your Steps does not matter. The steps get sorted by the $stepId:Number. All steps with the same $stepId get called at once. It starts the next stepId after all tasks within one StepId are completed.

$target is the class instance that throws the $eventToListen-Event once the task is completed.
$functionToCallToStart is a reference to the function that starts the task.

Example:
sequence.addStep(100, myAnim, myAnim.startAnimation, Event.COMPLETE);

At the moment there are two additional arguments that you can add.
//add a delay to the $functionToCallToStart
sequence.addStep(1.5, myAnim, myAnim.animateIn, Event.COMPLETE, {delay:5000});
//add arguments to pass to $functionToCallToStart
sequence.addStep(3, loader.contentLoaderInfo, loader.load, Event.COMPLETE, {functionToCallParams:new URLRequest('myFile.swf')});


The SimpleSequencer lets you sequence almost all tasks. And it's worked pretty well for me so far. I think I'm gonna blog some more examples and add more comments to the class soon. But for now, here is it:

SimpleSequencer.as

Let me know what you think.

Share this Post


                           

Comments


davidjmcclelland.com     Jan 10, 2009
Thanks for sharing this - it was in my back pocket when I needed it. You should mention sequence.start();

Thank You,

MAC

Rob Dodson     Nov 29, 2008
This is really neat Daniel, thanks for posting it!

Daniel Scheibel     Nov 11, 2008
@Patrick: i thought the stepId might be a good idea to add steps to the sequence whenever you want. i also use it to run tasks parallel. you can add multiple steps that have the same stepId. all steps that have the same stepId start running at the same time. the next step starts when all tasks are complete.

e.g.:
sequence.addStep(5, myAnim10, myAnim10.animateIn, Event.COMPLETE);
sequence.addStep(5, myAnim11, myAnim11.animateIn, Event.COMPLETE);
sequence.addStep(10, myAnim20, myAnim20.animateIn, Event.COMPLETE);


Patrick Matte     Nov 07, 2008
I just had to create the exact same thing. Although mine doesn't need the stepId, I just add the steps in the order that I want them. I also have a parallel sequencer so i can mix some parallel and sequential actions.

I was looking at a similar animation engine yesterday, it seems nice. http://code.google.com/p/kitchensynclib/



Speak






Submit »