views:

310

answers:

2

I get quite uncomfortable with code that I find on the Net like this

myTM.addEventListener("allTransitionsInDone", doneTrans);

(myTM is a TransitionManager instance.) What is the right way to get an event to fire when a non-motion transition (like a fade) is done? I usually use Event.EVENT_NAME but this one is not coming up.

+3  A: 

The constant for the event name is defined on the event type by convention, so eg, the "mouseDown" event is MouseEvent.MOUSE_DOWN.

However, 'allTransitionsInDone' specifically actually has no constant, since it is (sort of, see comments on livedocs) undocumented. Therefore there is no .ALL_TRANSITIONS_IN_DONE constant.

Simon Buchan
Yeah I saw that on LiveDocs... scary stuff. I guess once I figure out how to examine the source myself I'll find it less worrisome. Thanks Simon (again).
Yar
+1  A: 

Added to Simon's answer, if you want to avoid 'loosely referenced' event names and define your own ones, here's a few different propositions of conventional ways to use those:

1. Creating a custom event subclassing flash.events.Event

Probably the most conventional and obvious way even though being a bit of waste of code.

package transitions.events 
{
    import flash.events.Event;


    public class MyTransitionEvent extends Event 
    {

     public static const TRANSITION_INIT : String = "events.TransitionEvent.transitionInit";
     public static const TRANSITION_PROGRESS : String = "events.TransitionEvent.transitionProgress";
     public static const TRANSITION_COMPLETE : String = "events.TransitionEvent.transitionComplete";

     public function MyTransitionEvent(type : String, bubbles : Boolean, cancelable : Boolean)
     {
      super(type, bubbles, cancelable);
     }


     override public function clone() : Event
     {
      return new TransitionEvent(type, bubbles, cancelable);
     }
    }
}

2. Defining event names as static constants from within the event dispatcher (e.g. subclass of Sprite or MovieClip)

To make things clearer, the event types should exclusively be defined from within the class the events are dispatched.

package  
{
    import flash.events.Event;
    import flash.events.EventDispatcher;  


    public class Transition extends EventDispatcher
    {

     public static const TRANSITION_INIT : String = "events.TransitionEvent.transitionInit";
     public static const TRANSITION_UPDATE : String = "events.TransitionEvent.transitionUpdate";
     public static const TRANSITION_COMPLETE : String = "events.TransitionEvent.transitionComplete";

     public function Transition()
     {
     }


     private function init() : void
     {
      dispatchEvent(new Event(TRANSITION_INIT))
     }
    }
}

3. Reuse the native flash.events

Fortunately, the predefined events (i.e. flash.events.Event, flash.events.StatusEvent, flash.events.NetStatusEvent, etc.) are covering the most of the possible cases. So, for the sake of not reinventing the wheel and save some precious lines of code, if you find one matching your requirements, just reuse it.

package  
{
    import flash.events.Event;
    import flash.events.EventDispatcher; 


    public class Transition extends EventDispatcher
    {


     public function Transition()
     {
     }


     private function init() : void
     {
      dispatchEvent(new Event(Event.INIT));
     }

     private function change():void
     {
      dispatchEvent(new Event(Event.CHANGE));
     }

     private function complete():void
     {
      dispatchEvent(new Event(Event.COMPLETE));
     }
    }
}
Theo.T
I think that in the first one the constructor and the class should have the same name, no? Also, in the last example, you're saying that allTransitionsInDone is covered by one of the three "standard" events?
Yar
You're right for the constructor, just corrected it. Ironically the allTransitionsInDone doesn't seem to be defined as a standard event. Actually I've always felt uncomfortable with mx/fl transition lib. You've probably heard about Grants Skinners 'GTween'? http://www.gskinner.com/libraries/gtween/
Theo.T