View Full Version : reference to a function?
alterhut
08-25-2008, 05:37 PM
Hi,
I'm trying to make a reference to a function which will be called when a variable is changed. but it doesn't quite work.. check out the code:
class thisClass {
public var onChangeHandler:Function;
public var myValue:Number;
...
public function set value (foo:Number):void {
myValue = foo;
if (onChangeHandler != undefined) {
onChangeHandler();
}
}
}
this is not working, but I guess you know what I want to do... so how would it work?
newblack
08-25-2008, 06:39 PM
you have to actually define the function... are you doing that anywhere?
alterhut
08-25-2008, 07:18 PM
Hi, thanks for your reply.
I have multiple instances of this class, and the function call is only needed for *some* of them.
For those, I do define the function, like this:
instanceOfThisClass.onChangeHandler = myFunction;
...
public function myFunction ():void { ... }
For all others, no function is defined. And none should be called. Thats what this line is for: if (onChangeHandler != undefined) {
dan_hin
08-25-2008, 07:26 PM
I might be missing something here, but why not just use the function for every instance of a class, and then add a listener based on either a custom event or a conditional?
Dan
alterhut
08-25-2008, 07:30 PM
I need to assign different functions to different instances.
Is it possible to add a listener that waits for a variable to change? If yes, that would solve everything ;)
dan_hin
08-25-2008, 07:56 PM
it certainly is. If you use a timer, you could wait for a particular condition to become true. if/when it is, you add a listener. if the condition is false, then no listener is added and the instance remains "deaf" to the event.
newblack
08-25-2008, 08:07 PM
that should be fine how you're doing it...
alterhut
08-25-2008, 08:23 PM
that should be fine how you're doing it...
Uhm.. you're right! I must have a bug somewhere else. I tried this one and it's working:
package {
import flash.display.*;
import flash.text.*
public class test_file extends Sprite {
public var txt:TextField;
public var abc:Abc;
function test_file () {
txt = new TextField ();
txt.text = "It doesn't work! ;(";
addChild (txt);
abc = new Abc();
abc.func = myFunction;
abc.boom();
}
public function myFunction():void {
txt.text = "It works! :)";
}
}
}
class Abc {
public var func:Function;
public function boom ():void {
if (func != undefined) {
func();
}
}
}
Meanwhile I found the bug, I assigned the function to the wrong object. Shit happens ^^ ...
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.