PDA

View Full Version : Using .onRelease within a custom class


Ralph Brooker
08-23-2005, 07:44 PM
I am having trouble configuring a button handler from within a method of a custom class. The onRelease method will correctly call a Trace function, but it will not seem to call any other method in the custom class that requires parameters to be passed.

I am new to Flash and AS, so if I am making a basic error, please educate me! The objective is a reasonably portable class with interactivity that I can instantiate with minimum fuss.

Thank you!
Ralph

I have the following in the main timeline actions. The button exists in the library.
var bt:ButtonTest = new ButtonTest(this);
bt.setupButton(); //Works - button appears


The class file ButtonTest.as is as follows:
class ButtonTest{
// Test setting up buttons from within a custom class
var clipName:MovieClip;

function ButtonTest(clipNameParameter){
clipName = clipNameParameter;
}
function setupButton(){
clipName.attachMovie("freq_btn","myFreq_btn", clipName.getNextHighestDepth());
clipName.myFreq_btn.onRelease = function(){
trace("In the onRelease function: Button was pressed."); //Only this works
this.testFunction("A parameter string"); //Doesn't work!
this.testFunctionAlt(); //Doesn't work either
}
}
function testFunctionAlt(){
trace("In testFunctionAlt..."); // Never gets called
testFunction("Alternative parameter string");
}
function testFunction(t:String){
trace("In testFunction: t = " + t); // Never gets called
}
}

deadbeat
08-23-2005, 07:58 PM
Thats because the class methods aren't found within the scope of your button clip...easiest way around this is just to pass in a reference to the class itself, and use that to call the functions:

function setupButton(){
clipName.attachMovie("freq_btn","myFreq_btn", clipName.getNextHighestDepth());
clipName.manager=this;
clipName.myFreq_btn.onRelease = function(){
trace("In the onRelease function: Button was pressed."); //Only this works
this.manager.testFunction("A parameter string"); //Doesn't work!
this.manager.testFunctionAlt(); //Doesn't work either
}
}


Or look into using the Delegate class..

K.

senocular
08-23-2005, 07:59 PM
see:
http://www.kirupa.com/web/xml/XMLspecificIssues3.htm

different context, same problem

deadbeat beat me to the punch :D

Ralph Brooker
08-23-2005, 08:47 PM
Thank you both! Partial success... I couldn't get the .manager technique to work, but the Delegate class worked using this syntax:
clipName.myFreq_btn.onRelease = mx.utils.Delegate.create(this, testFunction );
But I can't seem to pass parameters to testFunction. I.e. if I use this syntax:
clipName.myFreq_btn.onRelease = mx.utils.Delegate.create(this, testFunction("hello") );
...
function testFunction(t:String){
trace("In testFunction, t = " + t );
}
then the output is :
In testFunction, t = undefined

Delegate doesn't seem to allow a third parameter to pass stuff either. Any thoughts? I could have a separate handler function for each button, but I will have many buttons and it seems rather inelegant.

Thank you!

deadbeat
08-23-2005, 08:55 PM
Sorry, I realize the error in my code, after looking at it again...should be like this:

function setupButton(){
var mc= clipName.attachMovie("freq_btn","myFreq_btn", clipName.getNextHighestDepth());
mc.manager=this;
mc.onRelease = function(){
trace("In the onRelease function: Button was pressed.");
this.manager.testFunction("A parameter string");
this.manager.testFunctionAlt();
}
}


K.

Xeef
08-23-2005, 09:01 PM
http://www.person13.com/articles/proxy/Proxy.htm



X=mx.utils.Delegate.create(_btn, testFunction);
function testFunction(t:String) {
trace("In testFunction, t = "+t+" "+this);
}
X("Hallo")

Ralph Brooker
08-23-2005, 09:55 PM
deadbeat -
Thank you - that works. Not "intuitively obvious to the casual observer," as my prof used to say, though. Do you have any suggestions for trace() techniques for debugging object path and context problems?

Thanks again - Ralph