Question

I have small swf as3 flash applications that are loaded by a main flex application.

The main Flex Application contains an SWFLoader and i added an event listener on SWFLoader.content to fetch custom events that I prepared.

now whenever I like in the application I use the function dispatchEvent to create the event and I make sure that the bubbles parameter of the event is true. so in this case the event bubbles up in the stage until it reaches my event listener and I can handle the event properly.

the problem resides when I have a static class and I want to dispatch the event from there. because it's a static class and not a display object that appears on my stage it has no where to bubble to and because of that I cannot fetch the custom even in my main flex application.

I tried using eventDispatcher with the following code:

            new EventDispatcher().dispatchEvent(new CustomEvent(CustomEvent.EVENT_setAttribute,{'attr_name':attrName,'attr_val':attrVal)));

The constructor of my custom event gets the parameters as the 2nd parameter.

so it seems that when I'm using event dispatcher, the event still has no where to bubble to and that is why I do not receive it in my main application as well.

how can I resolve the issue ? I need to be able to properly dispatch an event on objects that do not appear in the stage.

main I need to provide an object that appear on the stage for this function in order to use it to dispatch the event. I do hope that there is a better solution to resolve this issue.

any information would be greatly appreciated.

this is my custom event class:

public class CustomEvent extends Event
{
    public var command:String;
    public var params:Object;
    public static const CONTROL_TYPE:String = "eMyControl";


    private static var EVENT_setAttribute:String = "set_attribute";

    private static var gameAttributes:Object = new Object();

    public static function setAttribute(attrName:String,attrVal:String):void {
        gameAttributes[attrName]=attrVal;

        new EventDispatcher().(new XpoEvent(XpoEvent.EVENT_setAttribute,{'attr_name':attrName,'attr_val':attrVal}));
    }

    public function CustomEvent(eventName:String, params:Object = null, bubbles:Boolean = true, cancelable:Boolean = false)
    {
        var _loc_6:String = null;
        var _loc_5:String = "";
        if (params != null)
        {
            _loc_5 = "params: ";
            for (_loc_6 in params)
            {

                _loc_5 = _loc_5 + (_loc_6 + "=" + params[_loc_6] + " ");
            }
        }
        trace("CustomEvent issued - " + eventName + " " + _loc_5);
        super(CONTROL_TYPE, bubbles, cancelable);
        this.command = eventName;
        if (params != null)
        {
            this.params = params;
        }
        return;
    }

}

as you can see here I have a static function called setAttribute that passes attrName and Val to an array and then dispatches a custom event. by default the custom class event have bubbles enabled. it still seems that because I did not dispatch the event using an object that appears in the stage disallows from the main flex application to properly catch the event.

the following code does work:

  <object In Stage>.dispatchEvent(new CustomEvent("testme"));

thanks

Was it helpful?

Solution

When you create an EventDispatcher instance and dispatch something of it nobody will get your event because nobody can subscribe for it.

You have to dispatch an event from something which IS in display list. But your static class doesn't know anything about display list niether it can get access to it. You must provide an object in display list to it during initialization. For this purpose in loaded SWF you could pass someDisplayObject.root where someDisplayObject is on stage. Not someDisplayObject.stage because it will point to container Stage and what's more might be inaccessible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top