Brian Lin
Brian Lin

Reputation: 331

trouble while writing a var for function

I trying to to a function like this, but it couldn't work. could you have a look on this ^^

var test:testMc;

addMonster(test,testMc);

public function addMonster($mon:MovieClip,$monMc:MovieClip)
{$mon= new $monMc(stage, enemy);
}

Line 3 - 1067: Implicit coercion of a value of type Class to an unrelated type flash.display:MovieClip.

Line 6 - 1180: Call to a possibly undefined method $monMc.

How can I fix it?

Thank you for your help ^^

Upvotes: 0

Views: 68

Answers (1)

jhocking
jhocking

Reputation: 5577

In the future you should have posted more code and told us which lines those errors were on, because in this case we can't see where you declared testMc or the line numbers. Fortunately in this case it doesn't matter because I can see the problem at a glance.

The second parameter in addMonster() is typed MovieClip when in fact the object is a Class. That is the cause of both error messages. Your code should be something like:

public function addMonster($mon:MovieClip,$monMc:Class)

Incidentally, I'm very curious why you would write code like this. I guess this could be a small snippet from some sort of factory class, but at a glance this looks like a pretty goofy way to instantiate an object.

Upvotes: 2

Related Questions