Denoteone
Denoteone

Reputation: 4055

Custom eventListener Flash AS3

Not sure if this is the best option but thought maybe if I could use an eventlistener that would save some time. I am loading an XML file once a second and want there to be triggers if specific values are in the XML so far I have this:

//TIMER THAT LOADS XML ONCE A SECOND
var myXML:XML;
var myLoader:URLLoader = new URLLoader();

var myTimer:Timer = new Timer(100,2); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, runOnce);
myTimer.start();

function runOnce(event:TimerEvent):void {
    trace("HELLO");
myLoader.load(new URLRequest("myxml.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

}

function processXML(e:Event):void {
            trace("load XML");
            myXML = new XML(e.target.data);
            trace(myXML);
            }

Now I want event listners that do something if "direction" is equal to Left or Right:

addEventListener(myXML.LEFT, movieMC_Left);
addEventListener(myXML.LEFT, movieMC_Right);

//movieMC_Left and movieMC_Right functions that do something.

Upvotes: 0

Views: 406

Answers (2)

Plastic Sturgeon
Plastic Sturgeon

Reputation: 12527

Event types are just strings. So to listen for two different events you can do this:

addEventListener("XML left", onEventRightCalled);
addEventListener("XML Right, onEventLeftCalled);

For an extra level of type checking you can make your event types contants.

public static const XMLLEFT:String = "XML left";
public static const XMLRIGHT:String = "XML right";
addEventListener(XMLLEFT, onEventRightCalled);
addEventListener(XMLRIGHT, onEventLeftCalled);

So in your process XML function, you will loop thru you XML object looking for the direction variable. When you find what you are looking for, dispatch an event:

this.dispatchEvent(new Event(XMLLEFT, true));

One last thing: Be sure to make the listener functions, onEventRightCalled, and onEventLeftCalled.

Upvotes: 1

Bosworth99
Bosworth99

Reputation: 4234

I made a post a little while ago about implementing custom events. It may prove useful to you. I kind of get what you are doing here - but you are missing a key point, the dispatching of an event:

function processXML(e:Event):void {
    myXML = new XML(e.target.data);

    if (myXML.direction == "Left")
    {
       this.dispatchEvent(new XMLEvent(new XMLEvent.LEFT));
    }
    else
    {
    this.dispatchEvent(new XMLEvent(new XMLEvent.RIGHT));
    }
}

This assumes you've created a class that extends Event called XMLEvent (read my post if you have questions about that), and it contains the static const LEFT and RIGHT. It also assumes the class you are dispatching events from inherits from EventDispatcher (which it does if its a sprite or movieclip).

Otherwise = your setup ought to work.

What are you doing, exactly? Driving animation via XML? hmmm. Your logic is potentially flawed in that - if your load takes longer than a second (based on your time) you will start throwing errors.

Anyway - cheers

Upvotes: 2

Related Questions