BOB
BOB

Reputation: 31

ActionScript 3: Use an argument as a type?

I have a movie clip in my library linked with a class name "MyClass", and I am trying to do something like this in Actionscript 3:

function createbtn(bclass:Class):void{
   var addB:bclass = new bclass();
   addChild(addB);
}

creatbtn(MyClass);

But, I get this error: "1046: Type was not found or was not a compile-time constant: bclass."

Thank you very much in advanced.

Upvotes: 2

Views: 107

Answers (1)

Joony
Joony

Reputation: 4708

Close, the type of the variable is wrong.

function createbtn(bclass:Class):void{
  var addB:* = new bclass();
  addChild(addB);
}

creatbtn(MyClass);

Since you don't know the type, just mark it with a * so the compiler knows that it can be any type. You might want to do some type checking though, since you're adding it to the display list. Then you could probably type it as DisplayObject.

Upvotes: 6

Related Questions