PDA

View Full Version : problem accessing class properties from eventHandler inside class


bleepbloop
08-18-2004, 09:40 AM
HI

I have a class that is providing the code for a component that allows you to select a name from a List component inside my component. I am populating the internal mx.controls.List component ok with an array that is passed to my component as it is created (using createClassObject).

However, I would like to capture the 'change' event of my List component and do some stuff based on what has been selected. I can get the selected index of the List component ok from within my 'handleEvent' method, but I don't seem to be able to read any of the properties that are available throughout the rest of the code.

Line 75 returns undefined - even though I could read that value anywhere else in the code.

Can anyone guess why I cannot access SO_players_ar from inside my 'handleEvent' method? I also need to be able to make reference to another movieClip and change it's properties, and this also seems to be unavailable.

thanks in advance

class com.stardotstar.bleep.PlayerSelector extends mx.core.UIObject{

// class properties that allow instances of this class to be created
// at runtime using createClassObject method of the UIObject
var className:String = "PlayerSelector";
static var symbolOwner:Object = PlayerSelector;
static var symbolName:String = "PlayerSelector";

// class properties
private var _SO_players_ar:Array;

// instances used in this class
private var faceIcon_mc:com.stardotstar.x.FaceIcon;
private var emptyClip_mc:MovieClip;
private var listBox:mx.controls.List;


/* constructor function
*/
function PlayerSelector(){
trace("new Player selector");
}


// method: createChildren
function createChildren(Void):Void{
// add an empty movieClip that will then create the faceIcon component instance and the listBox instance
emptyClip_mc = createEmptyMovieClip("emptyClip_mc", 1);
emptyClip_mc.createClassObject(mx.controls.List, "listBox",1);
emptyClip_mc.createClassObject(com.stardotstar.x.F aceIcon, "faceIcon_mc", 2);
//populate the listBox
emptyClip_mc.listBox.dataProvider = SO_players_ar;
// SO_players_ar accessible here
trace("SOPA " + SO_players_ar[0].label);


}

// method: draw
function draw(Void):Void{
// position the listBox
emptyClip_mc.listBox._x = -10;
emptyClip_mc.listBox._y = -10;
emptyClip_mc.listBox.setSize(220, 110);

// add an eventListener to the listBox instance, calling the method handleEvent
emptyClip_mc.listBox.addEventListener("change", handleEvent );

// position the faceIcon_mc
emptyClip_mc.faceIcon_mc._x = 10;
emptyClip_mc.faceIcon_mc._y = 90;

// call size method
size();
}


// method that is called when the component is sized/resized
function size(Void):Void{
doLayout();
}


// method to do layout stuff
function doLayout(Void):Void{
}


// method to capture events from the listBox change event
function handleEvent(eObject:Object):Void{
var iSelectedIndex:Number = eObject.target.selectedIndex;
trace("handleEvent: " + eObject.target.__dataProvider[iSelectedIndex].label);

// SO_players_ar NOT accessible here
trace ("inside handleEvent : " + SO_players_ar[iSelectedIndex].label);
}


// get and set methods for the SO_players_ar Array
public function set SO_players_ar(players_ar:Array):Void{
_SO_players_ar = players_ar;
}

public function get SO_players_ar():Array{
return _SO_players_ar;
}



}{

bleepbloop
08-18-2004, 10:03 AM
it would appear that the answer lies somewhere in here http://www.actionscript.org/forums/showthread.php3?t=48953&highlight=eventHandler

but I can't work out how to apply it wahhhh! *cries*

bleepbloop
08-18-2004, 12:28 PM
WOW! I have found the answer. and it is typically obscure but there you go.

you need to use the 'Delegate.create' method of the mx.utils.Delegate class.

so my line that said

// add an eventListener to the listBox instance, calling the method handleEvent
emptyClip_mc.listBox.addEventListener("change", handleEvent );

is changed to

emptyClip_mc.listBox.addEventListener("change", Delegate.create(this, handleEvent));

but you must remember to import the mx.utils.Delegate class at the top of the class.


phew. you would not believe the amount of people that have been asking about this on the internet. Hopefully I can save a few headaches :)