0

From my main document class I pull a class which loads an xml file:

Main.as:

language = new Language();

Within my Language class I load an xml file using URLLoader and have an internal 'onComplete' function. What would I use to let my Main class know that this event is complete?

I've taken a look at the dispatchEvent() method and only ran into more trouble, probably using it wrong. Any help is greatly appreciated

Edit / Further Explanation:

From what it seems my Main class would contain:

addEventListener("languageLoaded", functionName);

I'm stuck with what my class needs. When I use:

dispatchEvent(new Event("customEvent"));

I have an undefined error. I've read that I need to extend EventDispatcher as my class but that seems off to me and there would be a different way to do this.

4

2 回答 2

1

In Main.as:

language = new Language();
language.addEventListener("languageLoaded", functionName);

Language need "extends EventDispatcher" as Main "extends Sprite".

In Language.as:

dispatchEvent(new Event("languageLoaded"));

but not

dispatchEvent(new Event("customEvent"));
于 2013-10-30T17:09:07.597 回答
0

More code may help to clarify to us what the problem might be. However it sounds like this would be more or less a solution:

Language.as:

public class Language
{
    private var m_uldr:URLLoader = new URLLoader();

    private function someFunction():void
    {
        m_uldr.addEventListener(Event.COMPLETE, myListener);
    }

    private function myListener(pEvent:Event):void
    {
    }

    public function addLoaderCompleteListener(pListener:Function):void
    {
        m_uldr.addEventListener(Event.COMPLETE, pListener);
    }
}

Main.as:

public class Main extends Sprite
{
    private var m_lang:Language = new Language();

    private function someFunction():void
    {
        m_lang.addLoaderCompleteListener(onLoaderComplete);
    }

    private function onLoaderComplete(pEvent:Event):void
    {
    }
}

If you call Language.someFunction and Main.someFunction before the URLLoader fires off the event, Language.myListener and Main.onLoaderComplete will both be called in response to the event.

于 2013-10-30T17:04:49.483 回答