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!
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); } }
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!
Don't see what you're looking for? Try a search.
|