Add multiple movie clips without replacing old ones

So, in short, this is my problem. I am using a variable which is a movie clip loaded from an external swf. I want to "spawn" multiple instances of a movie clip that all respond to the same code, so for example if I say var1.x = 100 they are all 100x. But my problem is that I run addChild (var1) several times (I don't actually type addChild (var1) over and over, I just add them at random times), the new child just replaces the old one, instead of creating multiple movie clips ... Should I be doing something like var var1: MovieClip var var2: MovieClip = new var1? (which doesn't work for me btw gives me errors)

Oh heres the code and also, I'm pretty new to as3 fyi, still don't even know how arrays work, which was my second guess of 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)
   }
  }

 }


}  

      

+2


source to share


6 answers


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 in the same instance, you are just reading yourself.

Also each instance is unique, you cannot globally change the x position of an instance by changing the other one. What would you do, since Henry suggests and adds each new MovieClip instance to the array, then whenever you change something, you can iterate over the array and apply the changes to each instance.



You cannot go var2: MovieClip = new var1 as var1 is an instance and not a class.

+1


source


Here's a different way of getting loaded MovieClips which I use when I need a lot of copies of an item.

in the swf you download, give the target movieclip the name of the link 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);
    }
 }

      

according to 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


source


It looks like you can access the exact same instance as in your code. It would be helpful to see how your code displays this.

If I wanted to load in one swf file and add MovieClip several times, I would put it in the library of this SWF file. Then create an instance and store it in an object pool or hash or some sort of 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 will contain a reference to the MySpecialThing class.

0


source


Calling addChild(var1)

multiple times for the same parent has no effect (unless you added another child to the same parent in between, in which case it will change the child index and return var1 to the beginning). If you call it on different parents, it will just change the parent var1, not duplicate it. Call addChild(new MovieClassName())

at any time instead to add new copies. Use an array as suggested here for accessing them later.

0


source


Wow, thanks Henry, just using an array did exactly what I needed and made things a lot easier.

0


source


when you load the loader you only get 1 instance, however you can do some funky reflection to figure out what class type is specified for a given loader.content

one and then create it with 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 it helps.

0


source







All Articles