PDA

View Full Version : Classes and XML.onLoad


Brucebannor
08-30-2007, 02:51 PM
I'm making a rather large class for a project. So keep in mind this is all in a class file, thus my scope issue. I know I need to pass the onLoad the instance of the class. I would normally just pass it thru the function. ie
dr_xml.onLoad = function(thisObj){} but this is reserved for whether it has loaded correcty and will rewrite the value to true / false. Any idea how to get my thisObj to be visible?

I also tried to overLoad the parameters of onLoad, so it would set the first to true / false, then pass my thisObj to no avail.



function myFunction()
{
trace(thisObj); // traces my Instance
dr_xml = new XML();
dr_xml.onLoad = function ()
{
trace(thisObj); // undefined
processXML(dr_xml);
}

}

function processXML()
{
// yadda yadda
}

xxneon
08-30-2007, 03:33 PM
use Delegate..


//...
dr_xml.onLoad = mx.utils.Delegate.create(this,classXmlLoad);
//...
function classXmlLoad()
{
trace(thisObj); // shouldn't be undefined..
processXML(dr_xml);
}
function processXML()
{
// yadda yadda
}

this should make it so thisObj should be able to be seen when onLoad is called..

or before the onLoad function of your original code.. add a property to the xml object..

dr_xml.thisObj = thisObj;
dr_xml.onLoad = function()
{
trace(this.thisObj); //shouldn't be undefined..
processXML(this.thisObj);
}

if you dont want to use Delegate..

Brucebannor
08-30-2007, 03:36 PM
makes very much sense.. i had a stupid moment, especially since I Delegate all of my other events. Thanks man

Brucebannor
08-30-2007, 06:54 PM
ok so same class and one more problem.. this is all part of my constructor function. No problem dropp'n this in its own .fla. But its dead to the world as soon as I stick it in the class, any suggestions? thanks


cbListener = new Object();

cbListener.change = Delegate.create(this, comboChange);
filter_cb.addEventListener("change", cbListener);

xxneon
08-30-2007, 06:58 PM
is cbListener a property of the class?

Brucebannor
08-30-2007, 07:00 PM
I declare it above the constructor as type object: var cbListener:Object;

xxneon
08-30-2007, 07:13 PM
and filter_cb is a property pointing to a combo box on the stage then correct?

Brucebannor
08-30-2007, 07:17 PM
yes sir, traced all of the components / objects and they all trace correctly.
If I disregard the Delgate like so:

cbListener.change = function()
{
trace("here");
}
filter_cb.addEventListener("change", cbListener);


its still dead to the world. Could this be a limitation of a class file in AS 2.0? I can't think of what I'm doing wrong, or any other ways to try

xxneon
08-30-2007, 07:23 PM
you could try this.. then you basically make the class the listener..
filter_cb.addEventListener("change", Delegate.create(this,comboChange));

is this class extending the MovieClip class by any chance?

Brucebannor
08-30-2007, 07:27 PM
ahhh already did. I think I'm gonna end up dropping back 10 and punting to a external as file instead of class. I have a datagrid that I assume will have the same problem.

and yup it extends MovieClip, would that be the problem?

xxneon
08-30-2007, 07:37 PM
in the constructor if you trace filter_cb.. does it outpout right?

Brucebannor
08-30-2007, 07:40 PM
uh-huh, I populate it w/ XML too so the combobox is there, thus my previous question haha. :eek:
trace(filter_cb + " / " + cbListener); //_level0.instance1.filter_cb / [object Object]

Brucebannor
08-31-2007, 12:23 PM
I know this isn't going anywhere but I made a very simplistic class to demonstrate the inability of the class to pick up events on flash components. I figured its easier to look at the code than to describe.

Notice I used three different methods of trying to catch the event. Any last insight or idea's?

If not, is this problem fixed on AS 3.0?

creynders
09-01-2007, 07:22 AM
If this code is executed in the constructor of your class the problem probably is that the component is not fully initialized when you call the addEventListener.
Try executing the code on onLoad of your class. like this:

//in constructor
onLoad = initCb;
//separate function
public function initCb(){
cbListener = new Object();
cbListener.change = Delegate.create(this, comboChange);
filter_cb.addEventListener("change", cbListener);
}

This only works if your class extends MovieClip.
I always address all my MovieClip and Component instances onLoad, just to be safe.