I am defining this function in one of my classes:
public function onUse():void {};
Then in another of my classes (let’s call it “class2”), I create a object of this class, and then want to override this function with another one. After some Google-Fu, I have found this, and used it…
button.onUse {
variable = value;
}
…but it executes instantly, and not when onUse() is called – which seems to be an empty function, always.
I didn’t find anything more than that – I tried a few things myself, like specifying a function inside class2 and using button.onUse = function();, but it always throws errors.
Can anyone tell me whether what I am trying to do is actually possible, and if it is, how can I do it?
You can only override functions when you are extending the class:
Hence, assigning a different function to a class method of an instance is not possible at runtime.
You can, however, let your original class contain a field of type
Function, and then simply assign a different closure to it.