Groups | Blog | Home
all groups > flash actionscript > july 2006 >

flash actionscript : scope problem within class with onPress



dnk NO[at]SPAM canada.com
7/22/2006 11:24:57 PM
Hi there.. I have a class in which there are properties (IE - _myColor).

Now suppose withing my class I have an onPress function defined...

IE

targetMc.myBtn.onPress = function():Void {
trace("The current value is: " + _myColor);
}

Now obviously this will not work because it is out of scope.

What is the proper way to access said property from the onPress that resides
IN the class file as well?

Thanks!
LuigiL
7/23/2006 12:00:00 AM
You can use a local reference to the current object or use the Delegate Class.
See attached examples.



//using the local reference

class TestClass {

private var test_mc:MovieClip;
private var my_btn:MovieClip;
private var test_str:String;

public function TestClass(target_mc:MovieClip){
test_mc=target_mc;
}

public function init():Void{
//local reference to the current object
var thisObj:TestClass=this;
test_str="This is a test";
test_mc.my_btn.onRollOver=function():Void{
thisObj.traceMessage();
//or
//trace(thisObj.test_str);
};
}

private function traceMessage():Void{
trace(test_str);
}
}

//using the Delegate Class
import mx.utils.Delegate;

class TestClass {

private var test_mc:MovieClip;
private var my_btn:MovieClip;
private var test_str:String;

public function TestClass(target_mc:MovieClip){
test_mc=target_mc;
}

public function init():Void{
test_str="This is a test";
test_mc.my_btn.onRollOver=Delegate.create(this,traceMessage);
}

private function traceMessage():Void{
trace(test_str);
}
}
myIP
7/23/2006 3:28:38 AM
It?s your event handler that is making things undesirable. In the link below,
LuigiL resolves this problem in an ?onLoad? event handler. If that doesn?t
work then goto the link I posted in this thread.


http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?catid=288&threadid=
1144407&highlight_key=y&keyword1=LuigiL%20

dnk NO[at]SPAM canada.com
7/23/2006 5:12:45 PM
That worked beautifully! I had been searching for some time yesterday and hte
other offered results never worked for me. I did not use the delegate in this
case (due to some other existing code), but will try the delegate after.

Thanks so much!
LuigiL
7/23/2006 8:48:45 PM
AddThis Social Bookmark Button