Reputation: 301
Ok, I have a class like this:
public class Foo extends Sprite {
public function Foo(x:Number, y:Number):void {
this.x = x;
this.y = y;
}
public function bar():void {
trace("I'm a happy class.");
}
}
And I want to do something like this:
var foo:Foo = new Foo();
foo.bar = function():void {
trace("I'm a happier class.");
}
I'm getting this error from the compiler: "Error: Illegal assignment to function bar". How can I change the public function bar on the fly?
Upvotes: 1
Views: 483
Reputation: 5495
You can't do that in ActionScript. There is a work around though, try something like this:
public dynamic class Foo{
public function Foo() {
this.bar = function():void { trace("bar"); }
}
}
var f:Foo = new Foo();
f.bar();
f.bar = function():void { trace("baz"); }
f.bar();
EDIT: OR THIS
public class Foo{
public var bar:Function;
public function Foo() {
this.bar = function():void { trace("bar"); }
}
}
var f:Foo = new Foo();
f.bar();
f.bar = function():void { trace("baz"); }
f.bar();
Goodluck!
Upvotes: 6