Nov
9
Sequencing in ActionScript 3

Write an XMLLoader, wait for an event.  Parse the xml, wait for some event.  Start up some animations, wait for an event.  Show some images. wait for an event.  Sounds pretty tedious doesn’t it?   Well if you’ve ever gotten tired of sequencing things here’s a simple class for you.

Below is a simple class I wrote that allows you to pass it an object and and event name to listen for and this class will execute the list one after another.  Each time waiting for the event to fire before beginning the next task.

The object must have a start() method and a getCompletEvent() method.  This is enforced by making that object implement the interface ISequenceable (posted at the end).   Also, each time a task is completed a SequenceEvent is dispatched.

package com.pj_co.core
{
        import com.pj_co.events.SequenceEvent;
        import com.pj_co.interfaces.ISequencable;
        import flash.events.Event;
        import flash.events.EventDispatcher;

        /**
        * @author Patrick Cousins
        */
        public class Sequence extends EventDispatcher
        {

                private var 	sequenceList	: Array		= [ ] ;
                private var 	funcList	: Array		= [ ] ;
                private var 	eventList	: Array		= [ ] ;
                private var	name		: String;
                private var	iterator	: int		= 0;
                private var	numItems	: int		= 0;

                public function Sequence ( name : String = "Unnamed")
                {
                        this.name = name;
                }

                public function addItem ( obj 	: ISequencable    ) : void
                {
                        trace ( "addItem", obj ) ;
                        sequenceList.push ( obj ) ;
                        funcList.push (  obj.start ) ;
                        eventList.push ( obj.getCompleteEvent() ) ;
                        numItems++;
                }

                public function execute ( ) : void
                {
                        var length	: int	= sequenceList.length;
                        trace("Sequence executing");
                        for ( var i : int = 0; i < length ; i++ )
                        {
                                if ( funcList [ ( i + 1 )] != null )
                                {
                                        sequenceList [ i ] .addEventListener ( eventList [ i ] , next ) ;
                                }
                                else
                                {
                                        sequenceList [ i ] .addEventListener ( eventList [ i ] , complete ) ;
                                }
                        }
                        next ( null ) ;
                }

                private function next ( e : Event ) : void
                {
                        var se 	: SequenceEvent	= new SequenceEvent( SequenceEvent.SEQUENCE_TICK );

                        se.index 		= iterator;
                        se.name 		= name;
                        se.obj			= sequenceList [ iterator ] ;

                        sequenceList [ iterator ] .start ( ) ;
                        dispatchEvent ( se ) ;
                        iterator ++;
                }

                private function complete ( e : Event ) : void
                {
                        var se 	: SequenceEvent	= new SequenceEvent ( SequenceEvent.SEQUENCE_COMPLETE ) ;
                        se.index 		= iterator;
                        se.name 		= name;
                        se.obj			= sequenceList [ iterator ] ;

                        dispatchEvent ( se ) ;
                        removeListeners ( ) ;
                }

                private function removeListeners ( ) : void
                {
                        var length : int = sequenceList.length;

                        for ( var i : uint = 0; i < length ; i++ )
                        {
                                if ( funcList [ ( i + 1 )] != null )
                                {
                                        sequenceList[ i ].removeEventListener ( eventList [ i ] , funcList [ ( i + 1 ) ] ) ;
                                        sequenceList[ i ].removeEventListener ( eventList [ i ] , next ) ;
                                }
                                else
                                {
                                        sequenceList[ i ].removeEventListener ( eventList [ i ] , complete ) ;
                                }
                        }
                }

                public function getEvents ( ) : Array
                {
                        return new Array ( ) ;
                }
        }
}

Here’s the Interface:

package com.pj_co.interfaces
{
        import flash.events.Event;
        import flash.events.IEventDispatcher;

        /**
        * Required methods for a class to be used by the sequencer
        * @author Patrick Cousins
        */
        public interface ISequencable extends IEventDispatcher
        {
                function start ( e : Event = null ) : void

                function getCompleteEvent () : String
        }

}


And finally here’s a download of all the code:


sequencing-in-as-3.zip






filed under: Misc, Scripts | permalink

4 Responses to “Sequencing in ActionScript 3”

  1. Max Says:
    January 5th, 2009 at 12:06 pm

    I think this would be the solution I’am searching for.

    Can you give me a short example how to use this class?

    Thanks.

  2. Max Says:
    January 5th, 2009 at 12:21 pm

    Sorry, it’s me again. What will the SquenceEvent look for? I couldn’t find it on your site.

    Thanks in advance.

  3. Chris G Says:
    January 29th, 2009 at 11:27 am

    Hi,

    I’m hoping to use this approach on a site as it looks ideal, but can’t find the code for the class:

    com.pj_co.events.SequenceEvent

    is there any chance you could post it?

    Thanks

  4. pj Says:
    March 4th, 2009 at 10:23 am

    Thanks for your comments. The full code as well as the sequence event has be posted. Sorry for the delay!

Leave a Reply

You must be logged in to post a comment.

© 2010 pajamacode | Theme by DemusDesign, Theme Lab, and Search Optimization | Powered by WordPress