Tuesday, August 5, 2014

Adding and Removing Many Similar Movie Clips at the Same Time

Adding lots of movie clips at once is useful in flash in you want to generate multiple objects such as scenery, enemies, etc. This can be done with a for loop.

First, set the ActionScript Linkage in your library for the movie clip that you want to add multiple times. For me, this linkage is "Raindrop."

for(i = 0;i < 25;i++) {
    //add the movie clip to the object
    //object is what the movie clip will be added under - this can be Stage at the highest level

    object.addChildAt(new Raindrop(),i);
}

To delete the movie clips you can use this code:

for(i = 0;i < 25;i++) {
    //get the unnamed movie clip with getChildAt
    //this parameter is 0, not i, because as one instance is deleted, the child numbers bump down.
    var temp:DisplayObject = object.getChildAt(0);
    //set the movieClip to null
    temp = null;
    //remove the movie clip
    object.removeChildAt(0);
}

This is good for adding and deleting, but what if you want to have these movie clips do something? Well you can do that to by creating an .as class. My .as class is named "RaindropClass." I have the Raindrop movieclip as a parameter, along with some other information that will be utilized in the code that makes the raindrop fall.

To start out you'll want to make an array to keep track of your objects
var rainDrops = new Array();

for(i = 0;i < 25;i++) {
    drop = new Raindrop();
    object.addChildAt(drop,i);
    raindrop = new RaindropClass(drop,startX,startY,fallSpeed);
    //add this raindrop to the raindrops array
    rainDrops.push(raindrop);
}

Then, to remove the raindrop objects you'll want to add this code in addition to the code above (the code above removes the movie clips, this removes the objects)

for each(var r:Raindrop in rainDrops) {
    r.remove();
    r = null;
 }

As for RaindropClass, the code could be as simple as this:

package  {
    import flash.display.MovieClip;
    import flash.events.Event;
   
    public class RaindropClass {
        private var drop:MovieClip;
        private var startX:Number;
        private var startY:Number;
        private var fallSpeed:Number;

        public function Raindrop(drop:MovieClip,startX:Number,startY:Number,fallSpeed:Number) {
            this.drop = drop;
            this.startX = startX;
            this.startY = startY;
           
            //set the raindrops start x and y
            this.drop.x = startX;
            this.drop.y = startY;
            this.fallSpeed = fallSpeed;
           
            //begin the fall function with enter frame event listener
            this.drop.addEventListener(Event.ENTER_FRAME,fall);
        }
       
        public function fall(event:Event) {
            this.drop.y = this.drop.y + this.fallSpeed;
            this.fallSpeed = this.fallSpeed + 0.5;
            if(this.drop.y >= 1000) {
                this.drop.y = this.startY;
                this.fallSpeed = 0;
            }
        }

    }
   
}

James Grams


No comments: