Add multiple movieclips, not replacing the old ones

So, in short, my problem is this. I am using a variable which is a movieclip loaded from an external swf. I want to "spawn" multiple instances of the movieclip that all react to the same code, so for example if I say var1.x = 100, they all are at 100x. But my problem is when I run addChild(var1) multiple times(I'm not actually typing in addChild(var1) over and over, I just have it set to add them at random times), the new child just replaces the old one, instead of making multiple movieclips. Should I do something like var var1:MovieClip var var2:MovieClip = new var1 ?(which doesnt work for me btw, gives me errors)

Oh, heres the code, and also, I am pretty new to as3 fyi, still don't even know how arrays work, which was my second guess to the problem.

var zombieExt:MovieClip;
var ldr2:Loader = new Loader();
ldr2.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded2);
ldr2.load(new URLRequest("ZombieSource.swf"));
function swfLoaded2(event:Event):void 
{
 zombieExt = MovieClip(ldr2.contentLoaderInfo.content);


 ldr2.contentLoaderInfo.removeEventListener(Event.COMPLETE, swfLoaded2);
 //zombieExt.addEventListener(Event.ENTER_FRAME, moveZombie)

 zombieExt.addEventListener(Event.ENTER_FRAME,rotate2);
 function rotate2 (event:Event)
 {
  var the2X:int = playerExt.x - zombieExt.x;
  var the2Y:int = (playerExt.y - zombieExt.y) * 1;
  var angle = Math.atan(the2Y/the2X)/(Math.PI/180);
  if (the2X<0) {
   angle += 180;
  }
  if (the2X>=0 && the2Y<0) {
   angle += 360;
  }
  //angletext.text = angle;
  zombieExt.rotation = (angle*1) + 90;
 }

 playerExt.addEventListener(Event.ENTER_FRAME,spawn1);
    function spawn1 (event:Event)
 {
  if(playerExt.y < 417)
  {
   var someNum:Number = Math.round(Math.random()*20);
    if(someNum == 20)
   {


    addChild(zombieExt)


    zombieExt.x = Math.round(Math.random()*100)
    zombieExt.y = Math.round(Math.random()*100)
   }
  }

 }


}  
13.10.2009 17:16:49
Can you post some code so we can see exactly what you are doing?
Reuben 13.10.2009 22:06:52
6 ОТВЕТОВ

It sounds like you might be accessing the same instance some how in your code. It would be helpful to see your code to figure this one out.

If I wanted to load in one swf files and add a MovieClip multiple times I would place it in the library of that SWF file. And then instantiate it and store it into an object pool or a hash or some list.

// after the library were finished loading
var list:Array = [];
for(var i:int=0; i<10; i++) {
   var myCreation:MySpecialThing = new MySpecialThing();
   addChild(myCreation);
   list.push(myCreation);
}

where my library would contain a linkage to the class MySpecialThing.

0
13.10.2009 19:47:21

addChild() does not create new instances. It is used to add an already created instance to the display list. If you call addChild() multiple times on the same instance then you are just readding itself.

Also each instance is unique, you can not globally change the x position of an instance by changing another one of them. What you would do is as Henry suggests and add each new instance of a MovieClip into an array, then whenever you change something you can loop through the array and apply the changes to each instance.

You can not go var2:MovieClip = new var1 either since var1 is an instance and not a class.

1
13.10.2009 22:57:42

Calling addChild(var1) multiple times on the same parent doesn't have any effect (unless you have added another child to the same parent in between, in which case it will change the child index and bring var1 to the top). If you call it on different parents, it will just change the parent of var1, doesn't duplicate. Call addChild(new MovieClassName()) at random times instead to add new copies of it. Use an array as suggested here to access them later.

0
14.10.2009 06:29:56

Wow, thanks there henry, just using an array did exactly what I needed, and made things alot simpler.

0
14.10.2009 17:10:40

Here's a different method of receiving loaded MovieClips, which i use when i need many copies of the item.

in the swf you are loading, give the target movieclip a linkage name in the library, for this example i will use "foo"

 private var loadedSwfClass:Class
 private var newZombie:MovieClip;
 private var zombieArray:Array = new Array();

 function swfLoaded2(event:Event):void 
 {
    loadedSwfClass = event.target.applicationDomain.getDefinition("foo");

    for(var n:int = 0; n<100; n++){
       newZombie = new loadedSwfClass()
       zombieArray.push(newZombie);
       addChild(newZombie);
    }
 }

as per this tutorial http://darylteo.com/blog/2007/11/16/abstracting-assets-from-actionscript-in-as30-asset-libraries/

although the comments say that

 var dClip:MovieClip = this;
 var new_mc = new dClip.constructor();
 this.addChild(new_mc);

will also work.

1
15.10.2009 14:13:45

when you load in using a loader you only get 1 instance, however you can do some funky reflection to determine what class type the given loader.content is, and then instantiate them using that. For Example:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_completeHandler);
loader.load(new URLRequest("ZombieSource.swf"));

var classType:Class;

function loader_completeHandler(evt:Event):void
{
    var loadInfo:LoaderInfo = (evt.target as LoaderInfo);
    var loadedInstance:DisplayObject = loadInfo.content;

    // getQualifiedClassName() is a top-level function, like trace()
    var nameStr:String = getQualifiedClassName(loadedInstance);

    if( loadInfo.applicationDomain.hasDefinition(nameStr) )
    {
        classType = loadInfo.applicationDomain.getDefinition(nameStr) as Class;
        init();
    }
    else
    {
        //could not extract the class
    }
}

function init():void
{
    // to make a new instance of the ZombieMovie object, you create it
    // directly from the classType variable

    var i:int = 0;

    while(i < 10)
    {
        var newZombie:DisplayObject = new classType();
        // your code here 
        newZombie.x = stage.stageWidth * Math.random();
        newZombie.x = stage.stageHeight * Math.random();

        i++;
    }

}

Any problems let me know, hope this helps.

0
20.10.2009 22:53:08