I have a custom class, and within' that class I'm doing an XML load- I'd like to broadcast to a custom Event for my class from within the _xml.onLoad statement, however... its not letting me do so. I remember, sometimes, when using an event inside a class, you have to create a reference to your class instance and use that to access properties/methods of your class within the event handler, however, that's not working in this situation. See my code below to understand what i'm talking about: ---------------------------------- Class Code: ---------------------------------- class SomeClass { var broadcastMessage:Function; var addListener:Function; var _xml:XML; function someClass () { // Set up the AsBroadcaster AsBroadcaster.initialize(this); addListener(this); // Create a reference to your class instance _class = this; // Do the XML Code and Load the file. _xml = new XML(); _xml.onLoad = myFunction; _xml.load("file.xml"); } // End function function myFunction(success) { if (success) { _class.broadcastMessage("onStatus","xml loaded"); // The following trace comes back undefined: trace("class: " + _class) } // End if } // End function } // End class ---------------------------------- Frame Code: ---------------------------------- var myClass:SomeClass = new SomeClass(); var listener_obj:Object = new Object(); listener_obj.onStatus = function (msg:String) { trace("msg: " + msg); } // End function myClass.addListener(listener_obj);
UPDATE: I found a solution that works, however, if somebody could explain why this works and the other doesn't, it'd be appreicated. See updated code below: ---------------------------------- Class Code: ---------------------------------- class SomeClass { var broadcastMessage:Function; var addListener:Function; var _xml:XML; function someClass () { // Set up the AsBroadcaster AsBroadcaster.initialize(this); addListener(this); // Create a reference to your class instance _class = this; loadXML(); } // End function private function loadXML() { // Do the XML Code and Load the file. _xml = new XML(); // For some reason, the below statement is REQUIRED // in order for it not to be "undefined" in the onLoad handler. var _class = this; _xml.onLoad = function (success) { if (success) { _class.broadcastMessage("onStatus","xml loaded"); // The following trace comes back undefined: trace("class: " + _class) } // End if } // End function _xml.load("file.xml"); } // End function } // End class ---------------------------------- Frame Code: ---------------------------------- var myClass:SomeClass = new SomeClass(); var listener_obj:Object = new Object(); listener_obj.onStatus = function (msg:String) { trace("msg: " + msg); } // End function myClass.addListener(listener_obj);
When you create a variable in a function, it will only exist as long as the function runs, and gets deleted when the function ends. So the line inside the constructor creates a variable 'class' that only exist in the instantiation, outside that '_class' will be undefined. The same goes for the line in the function loadXML(), but that's the place where the var gets used, so now it's working. You could also make the _class var a class variable and assign the reference in the constructor, then the var would be available as long as the object exists. hth, blemmo
Thank-you for your solution, however, that is actually a mistake in my post. In my actual project, i had defined the _class var outside of the constructor.
Inside call back handlers the class members are out of scope, including the var _class you declared. In your code in the function loadXML() the declaration var _class=this; creates a new (!) variable with a scope local to the function holding a reference to the current object/class.
Don't see what you're looking for? Try a search.
|