views:

16

answers:

1

Is it possible to grab an event, hold it in a class property, do some action, then, when that action is complete, dispatch an event and then retrieve the original event.target in the listener?

I am trying to avoid using a custom event for now, as it is giving me massive amounts of heartburn when dispatching it from a loaded SWF.

Here is some code (that doesn't work) that hopefully will help to understand what I am attempting to do:

package
{
    import fl.transitions.easing.Regular;
    import fl.transitions.Tween;
    import fl.transitions.TweenEvent;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    public class Document extends MovieClip
    {
        protected var _event:Event;

        public function Document():void
        {
            initLoader();
        }

        protected function initLoader():void
        {
            var loader:URLLoader = new URLLoader();
            loader.load(new URLRequest('some_url.xml'));
            loader.addEventListener(Event.COMPLETE, intermediaryHandler);
        }

        protected function intermediaryHandler(event:Event):void
        {
            _event = event;
            doSomeAction();
        }

        protected function doSomeAction():void
        {
            var tween:Tween = new Tween(someObject, "alpha", Regular.easeIn, 0, 1, 5);
            tween.addEventListener(TweenEvent.MOTION_FINISH, tweenHandler);
        }

        protected function tweenHandler(event:TweenEvent):void
        {
            /*
             * Imagine some object is listening to this class for Event.COMPLETE.
             * I would like to retrieve the event.target that I could have retrieved
             * in the `intermediaryHandler()` method, in the listener to the following
             * event being dispatched:
             */
            dispatchEvent(_event);
        }
    }
}

I am able to stash the event in a class variable, and then access it later, but the cherry on the icing would be to get it from the event fired withing tweenHandler() in my example above.

Edit Well, it doesn't look possible (thanks @bhups for the explanation). I am currently storing the retrieved event in a property _event and then getting it at a later time when I need it. This seems to work, but isn't wonderful.

+1  A: 

I am afraid it is not possible. Whenever you are re-dispatching an Event, then Flash Runtime makes a clone of it and dispatches it into the Event Flow. And once Event goes to the Event Flow then, its properties can not be altered. target and currentTarget are read-only properties of an Event object. So without using a CustomEvent it is not possible to achieve what you are trying to do.
What problem exactly you're facing when using Custom Event?

bhups
Thanks @bhups. I receive error #1034 when I try to dispatch a custom event from a loaded SWF. I have not been able to solve the issue as of yet. It is similar to when I try to get or set a custom type within a loaded SWF. I think it might have something to do with the security sandbox, but I am not sure.
letseatfood
I may have found two different solutions here: http://www.jeffdepascale.com/index.php/flash/custom-events-in-loaded-swf-files/ and here: http://www.jeffdepascale.com/index.php/flash/dispatching-custom-events-between-child-swfs-in-as3/
letseatfood