1
votes

I'd like to add a movieclip from the library to a movieclip on the stage.

function setMc(con:Sprite,mc:Sprite):void
{
    con.addChild(mc)
    mc.x=mc.width/2
    mc.y=mc.height/2
}        

setMc(myholder,mylibrarymc)

I get this error:

TypeError: Error #1034: Type Coercion failed

What do I need to change?

3
Can you paste the full error and tell us what statement throws the error?Florent

3 Answers

3
votes

Library only contains prototypes, not actual objects. I assume "mylibrarymc" is a name of the MC type in library. In this case "mylibrarymc" is type Class, which is used differently.

function setMc(con:Sprite,mc:Class):void
{
    var newMC:DisplayObject=new mc() as DisplayObject;
    // Here you actually make an object out of a class
    con.addChild(newMC);
    newMC.x=newMC.width/2;
    newMC.y=newMC.height/2;
}        

setMc(myholder,mylibrarymc);

Hope this helps. It's been quite some time I dabbled with libraries.

1
votes

Your function expects a Sprite, and you're probably passing it a MovieClip. Have a look at the "export for actionscript" options you have when right click on the mc in the library

0
votes

Right click the object in the library and go to 'Properties'. Make sure the box labelled "Export for ActionScript" is ticked and assign it an appropriate name. The class name you give the symbol is what you would use to create an object of that type in the code itself, for example:

Export a symbol for ActionScript with the Class name "Player".

In your ActionScript file:

var player = new Player();
addChild(player);

You can manipulate the object using any of the MovieClip member functions (position, alpha) and assign event listeners to make it interactive.