views:

41

answers:

2

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?

+1  A: 

I think the class needs to be declared as dynamic

sberry2A
+4  A: 

Hi Ricardo,

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!

Tyler Egeto