Removing movie clips in arrays

Hi there
I have this code in my Flash file:
for(bubble in bubbleArray){
     bubbleArray[bubble]._y -= 3;
     if(bubbleArray[bubble]._y < 370){
          bubbleArray[bubble].removeMovieClip();
I'm wondering, when I remove the movie clips should I also be popping them from the array?
I have an onEnterFrame function that attaches the movie clips and pushes them into the array, so I'm worried that if I don't pop them the array will continue to increase in size and slow down the movie. Is this the case, or will they be automatically removed from the array by the removeMovieClip() method above?

Looks like I spoke too soon. I imagined that removing them from the array would be easy, but I'm struggling.
The movie clip that is removed from its parent clip and should be removed from the array will not always be the first element in the array, and I'll need to remove multiple clips at a time. I've put in a trace action to trace the length of my array, and after about ten seconds it contains about 2500 elements, most of which have already been removed using the removeMovieClip() method. I'm worried this will slow down the movie.
So to clarify: How can I remove these elements from my array when I remove them from the stage?
I tried to include something like:
while(bubbleArray[0] == undefined){
     bubbleArray.shift();
but it caused Flash Player to freeze up. Also, if I trace bubbleArray[0] it is only "undefined" before something is pushed into the array. Once the clip has been removed from the stage, the trace just returns a blank line, not "undefined".
I'd appreciate any help.
Here is all my code:
stop();
splash.stop();
var i:Number;
var j:Number = 0;
var minBubbles:Number = 10;
var maxBubbles:Number = 20;
var bottomBubbleLimit:Number = 0;
var topBubbleLimit:Number = 1;
var bubbleNumber:Number;
var bubbleX:Number;
var bubbleY:Number;
var bubbleScale:Number;
var bubbleMinScale:Number = 30;
var bubbleMaxScale:Number = 100;
var fewerBubbles:Number;
var ring:String;
var ringArray:Array = [ringAnimation.ring1, ringAnimation.ring2];
var bubbleArray:Array = [];
ringAnimation.onEnterFrame = function(){
    for(ring in ringArray){
        if(ringArray[ring]._y > 370){
            bubbleNumber = (minBubbles + (Math.random() * (maxBubbles - minBubbles)));
            for(i = 0; i < bubbleNumber; i++){
                bubbleX = ((ringArray[ring]._x - (ringArray[ring]._width / 2)) + (Math.random() * ringArray[ring]._width));
                bubbleY = ((ringArray[ring]._y - (ringArray[ring]._height / 2)) + (Math.random() * ringArray[ring]._height));
                bubbleScale = bubbleMinScale + (Math.random() * (bubbleMaxScale - bubbleMinScale));
                ringAnimation.attachMovie("bubble", "bubble" + String(j), this.getNextHighestDepth(), {_x:bubbleX, _y:bubbleY, _xscale:bubbleScale, _yscale:bubbleScale});
                bubbleArray.push(ringAnimation["bubble" + String(j)]);
                j++;
    for(bubble in bubbleArray){
        bubbleArray[bubble]._y -= 3;
        if(bubbleArray[bubble]._y < 370){
            bubbleArray[bubble].removeMovieClip();
    trace(bubbleArray.length);
    if(ringAnimation._currentFrame == 6){
        splash.gotoAndPlay(1);
    }else if(ringAnimation._currentFrame == 12){
        splash.gotoAndPlay(1);
fewerBubbles = setInterval(reduceBubbles, 600);
function reduceBubbles(){
    if(minBubbles > bottomBubbleLimit){
        minBubbles--;
    if(maxBubbles > topBubbleLimit){
        maxBubbles--;
    if(minBubbles == bottomBubbleLimit && maxBubbles == topBubbleLimit){
        clearInterval(fewerBubbles);

Similar Messages

  • Help! Remove Movie Clips

    OK...here's my problem... I am making a portfolio site and I
    have my thumbnails being created dynamically through XML...very
    similar to the galleries example in the Sample and Tutorials. Only
    problem is that I have multiple keyframes with different thumbnails
    that need to be displyed through a different XML file. I got it to
    work but the thumbnails from the first frame are still showing on
    the second frame...How do I remove the dynamically created movie
    clips?

    OK...here's the code...At thebottom of the document, I need
    to remove the clips created on the next button. This is the same
    code from the gallery sample file; I just added my own XML file.
    stop();
    import mx.transitions.*;
    _global.thisX = 30;
    _global.thisY = 70;
    _global.stageWidth = 600;
    _global.stageHeight = 400;
    var gallery_xml:XML = new XML();
    gallery_xml.ignoreWhite = true;
    gallery_xml.onLoad = function(success:Boolean) {
    try {
    if (success) {
    var images:Array = this.firstChild.childNodes;
    var gallery_array:Array = new Array();
    for (var i = 0; i<images.length; i++) {
    gallery_array.push({src:images
    .firstChild.nodeValue});
    displayGallery(gallery_array);
    } else {
    throw new Error("Unable to parse XML");
    } catch (e_err:Error) {
    trace(e_err.message);
    } finally {
    delete this;
    gallery_xml.load("gallery_practices2.xml");
    function displayGallery(gallery_array:Array) {
    var galleryLength:Number = gallery_array.length;
    for (var i = 0; i<galleryLength; i++) {
    var thisMC:MovieClip =
    this.createEmptyMovieClip("image"+i+"_mc", i);
    mcLoader_mcl.loadClip(gallery_array.src, thisMC);
    preloaderMC = this.attachMovie("preloader_mc",
    "preloader"+i+"_mc", 5000+i);
    preloaderMC.bar_mc._xscale = 0;
    preloaderMC.progress_txt.text = "0%";
    thisMC._x = _global.thisX;
    thisMC._y = _global.thisY;
    preloaderMC._x = _global.thisX;
    preloaderMC._y = _global.thisY+20;
    if ((i+1)%5 == 0) {
    _global.thisX = 20;
    _global.thisY += 80;
    } else {
    _global.thisX += 80+20;
    var mcLoader_mcl:MovieClipLoader = new MovieClipLoader();
    var mclListener:Object = new Object();
    mclListener.onLoadStart = function() {
    mclListener.onLoadProgress = function(target_mc, loadedBytes,
    totalBytes) {
    var pctLoaded:Number =
    Math.round(loadedBytes/totalBytes*100);
    var preloaderMC =
    target_mc._parent["preloader"+target_mc.getDepth()+"_mc"];
    preloaderMC.bar_mc._xscale = pctLoaded;
    preloaderMC.progress_txt.text = pctLoaded+"%";
    mclListener.onLoadInit = function(evt:MovieClip) {
    evt._parent["preloader"+evt.getDepth()+"_mc"].removeMovieClip();
    var thisWidth:Number = evt._width;
    var thisHeight:Number = evt._height;
    var borderWidth:Number = 2;
    var marginWidth:Number = 8;
    evt.scale = 8;
    evt.lineStyle(borderWidth, 0x000000, 100);
    evt.beginFill(0xFFFFFF, 100);
    evt.moveTo(-borderWidth-marginWidth,
    -borderWidth-marginWidth);
    evt.lineTo(thisWidth+borderWidth+marginWidth,
    -borderWidth-marginWidth);
    evt.lineTo(thisWidth+borderWidth+marginWidth,
    thisHeight+borderWidth+marginWidth);
    evt.lineTo(-borderWidth-marginWidth,
    thisHeight+borderWidth+marginWidth);
    evt.lineTo(-borderWidth-marginWidth,
    -borderWidth-marginWidth);
    evt.endFill();
    evt._xscale = evt.scale;
    evt._yscale = evt.scale;
    evt._rotation = Math.round(Math.random()*-10)+5;
    evt.onPress = function() {
    this.startDrag();
    this._xscale = 30;
    this._yscale = 30;
    this.origX = this._x;
    this.origY = this._y;
    this.origDepth = this.getDepth();
    this.swapDepths(this._parent.getNextHighestDepth());
    this._x = (_global.stageWidth-evt._width+30)/2;
    this._y = (_global.stageHeight-evt._height+30)/2;
    mx.transitions.TransitionManager.start(this,
    {type:mx.transitions.Photo, direction:0, duration:1,
    easing:mx.transitions.easing.Strong.easeOut, param1:empty,
    param2:empty});
    evt.onRelease = function() {
    this.stopDrag();
    this._xscale = this.scale;
    this._yscale = this.scale;
    this._x = this.origX;
    this._y = this.origY;
    evt.onReleaseOutside = evt.onRelease;
    mcLoader_mcl.addListener(mclListener);
    next_btn.onRelease= function() {
    gotoAndStop(2);
    back_btn.onRelease= function() {
    gotoAndStop(1);
    }

  • OOP and movie clips in arrays

    I have a number of movieclips in a game that needs to all appear at a certain x;y co-ordinate at the same alpha value and move at a dynamically produced path and basically have the same behaviour up to a point. Flash has created links to .as files for eachEach movie clip.
    I want to put them all in an array to code once. My question - Must this array be in the main class .as file or can I have a seperate .as file written especially for this array.
    Thanks
    Charine
    Facebook: buggoop

    you can pass a reference from your main class to your array class. eg,
    // in document class
    var arrayClass:ArrayClass=new ArrayClass(stage);
    // ArrayClass
    package{
    public class ArrayClass{
    private var _stage:Stage;
    public function ArrayClass(s:Stage){
    _stage=stage;
    dowhatever();
    private function dowhatever():void{

  • Remove duplicate clips through array

    I'm using beneath code to two areas of clips. On the left 10 clips consisting of 2 columns and 5 rows. On the right the exact duplicate. It starts of with clip 1 on the left, than it's duplicate on the right, then clip 2 on the left, than it's duplicate on the right etc. Until both left and right have the exact same 10 clips in the same order.
    Now I've added an event listener. The way I would like it to work is when I click e.g. clip 4 on the left, not only that clip should be removed, but also it's counterpart clip 4 on the right. And the same the other way: clicking clip 4 on the right should remove both the right clip 4 as the left clip 4.
    The way I've got it working now is only when clicking clips on the left. That removes that clip and the clip on the right. I'm using the indexOf array way for that. By deleting the clip on that indexOf location, which would be a left clip and the clip on the right by adding 1 to that indexOf. Since in the array the right clip (duplicate) immediately follows the left (original) clip.
    Of course that doesn't work the other way, when I click a right clip first. Since I then would have to subtract 1 of the indexOf location to get to the left clip version. But I can't think of a way to determine whether the clip clicked on is on the right area or the left. I've commented that second removeChild line since that doesn't work when clicking on a right clip.
    Can anyone thing of a way?
    var clipcopies:Array = new Array();
    for (var rows:uint=0; rows <5; rows++)
              for (var cols:uint= 0; cols <2; cols++)
                        for (var cops:uint=0; cops <2; cops++)
                                  var persona:clips = new clips();
                                  persona.scaleX = .5;
                                  persona.scaleY = .5;
                                  persona.gotoAndStop(cols+1+rows*2);
                                  persona.x = (cols * (persona.width+20) + 10)+(cops*300);
                                  persona.y = (rows * (persona.height+20) + 10);
                                  persona.addEventListener(MouseEvent.CLICK,clickPersona);
                                  addChild(persona);
                                  clipcopies.push(persona);
    function clickPersona(e:MouseEvent):void
              var thisclip:uint = clipcopies.indexOf(e.target);
              trace(thisclip);
              removeChild(clipcopies[thisclip]);
              //removeChild(clipcopies[thisclip+1]);

    If I've understood your code correctly, clips on the left will have even numbered indexes or 0 as their index.  Clips on the right will have odd numbered indexes.  I would try:
    var clipcopies:Array = new Array();
    for (var rows:uint=0; rows <5; rows++)
              for (var cols:uint= 0; cols <2; cols++)
                        for (var cops:uint=0; cops <2; cops++)
                                  var persona:clips = new clips();
                                  persona.scaleX = .5;
                                  persona.scaleY = .5;
                                  persona.gotoAndStop(cols+1+rows*2);
                                  persona.x = (cols * (persona.width+20) + 10)+(cops*300);
                                  persona.y = (rows * (persona.height+20) + 10);
                                  persona.addEventListener(MouseEvent.CLICK,clickPersona );
                                  addChild(persona);
                                  clipcopies.push(persona);
    function clickPersona(e:MouseEvent):void
              var thisclip:uint = clipcopies.indexOf(e.target);
              trace(thisclip);
              removeChild(clipcopies[thisclip]);
             if (thisclip % 2 > 0) // then thisclip is an odd uint - ie clip is on the right
                   removeChild(clipcopies[thisclip-1]); // remove the clip on the left
              }else{  // then thisclip is an even uint - ie clip is on theleft
                   removeChild(clipcopies[thisclip+1]); // remove the clip on the right

  • Removing movie clip in a frame

    Hello,
    I have a timeline with 12 frames, in frame 7 I added a movie clip to the  stage:
    var pContainer:MovieClip = new MovieClip;
    addChild(pContainer);
    var myLoader1:Loader = new Loader();
    pContainer.addChild(myLoader1);
    var myRequest1:URLRequest = new URLRequest("flash/products.swf");
    myLoader1.load(myRequest1);
    pContainer.x = stage.stageWidth/2 - 380;
    pContainer.y = stage.stageHeight/2 - 180;
    now, when I hit a button and I go back to frame 2 or any other frame in my movie, this movie clip is on  top of everything,
    I tried in frame 2:
    removeChild(pContainer);
    but it doesn't remove the movie clip any ideas?

    Managing things you create dynamically can be tricky business... I don't yet know why.
    One way of doing this is to manually plant pContainer as an empty movieclip symbol in frame 7 rather than creating it dynamically.
    Another thing to try would be to use the removeChild command before you move away from frame 7.  So you might need to have a conditional in your button that tests the currentFrame value and then executes the removeChild command before moving to the destination frame.

  • Selecting a random movie clip from array

    I know this should be easy, but I can't seem to make it work. All I want is for the variable currentPage to select randomly from an array so that every time the page loads, it displays a different currentPage.
    This is what I have so far:
    var myImages:Array = new Array("outsource_mc","solutions_mc","staff_mc");
    var randomImages:Array = [];
    var randomCount:Number = 1;
    var r:Number;
    for (var i = 0; i<randomCount; i++) {
    r = Math.floor(Math.random()*myImages.length);
    randomImages[randomImages.length] = myImages.splice(r, 1);
    trace(randomImages);
    currentPage = the random movie clip;
    Thank-you for any help!!

    Anytime you want to see what something is, use the trace() function... it is an essential code design tool that outputs whatever you ask it to in the Output window....  trace(currentPage);
    For the code I showed currentPage would have been one of the instance name String values from the array.  If you had taken the quotes off of the names in the array, it would be a reference to the actual instance.
    As far as the new code you show, I don't know what you are trying to do, nor what it is not doing that you expect it to, but the last two lines have no relationship to the lines preceding it.

  • HELP WITH AS3 Removing Movie Clip

    every code i try in the action script to remove a movie clip off of the stage does not work. HELPPPP.

    well this is my code to bring it onto the stage
    import flash.events.MouseEvent;
    pills.addEventListener(MouseEvent.CLICK, arrest)
    function arrest(pEvent:MouseEvent):void{
              if(pEvent.target==pills){
                        var carimage: policecar= new policecar();
                        carimage.x=0;
                        carimage.y=0;
                        this.addChild(carimage);
    and the remove is under a different clip
    stop();
    bars1.addEventListener(MouseEvent.ROLL_OVER, barsout)
    function barsout(pEvent:MouseEvent):void{
              if(pEvent.target==bars1){
                        this.removeChild(carimage);
    even when i add the latter code into the origininal code above it tells me access of undefined property (carimage)

  • Removing movie clips with added children

    If I have a movie clip added as a child to another movie clip
    on the stage, and I remove it's parent, and null all references to
    the parent, will both the parent and child movie clip be deleted by
    the garbage collector? If so, will it be done on it's regular pass
    or by mark and sweep?

    OK, so if I add a movie clip like this:
    function addMovie()
    var toAdd:MovieClip = new Example(); // example extends
    movieclip
    this.addChild(toAdd);
    I'm assuming that toAdd will be nulled when the function is
    complete, because it's a local variable. Then if I put code inside
    the movie clip at a certain frame or in a function in the class
    Example to get rid of it:
    this.parent.removeChild(this);
    I'm assuming that's all i need to do to get rid of it. I
    can't null the instance of the class Example using this = null from
    inside it, and the only reference to it is the child reference from
    the parent.
    Am I right or is there more I need to do to make the instance
    of example eligible for GC?

  • Remove Movie Clip

    I would like to have the user click on a button or movie clip
    that takes them to a new scene with multiple questions on it, and
    depending on what answer they choose, come back to the main scene
    and the button be gone, and replaced with a graphic (that won't
    have any function). I thought I could change the alpha of the
    graphic (sallylock_mc) from 0 to 100, but its only staying on 0.
    And, I can't figure out how to make sally_mc disappear. So far,
    here's what I have:
    Main scene:
    stop();
    sallylock_mc._alpha=0
    sally_mc.onRelease=function(){
    _root.gotoAndPlay("sally");
    "sally" scene:
    stop();
    sallyright_btn.onRelease=function(){
    _root.gotoAndPlay("main");
    sallylock_mc._alpha=100

    OK :)
    sally_mc (I changed it to a button, so now it will be
    refrenced as sally_btn) exists in the very first frame of the first
    scene ("main")of the movie. You have to click it to go to the scene
    called "sally" Within the scene called "sally", there is a question
    and answers. When the user clicks the right answer, I want them to
    go back to the "main" scene, and I want sally_btn to be gone, and
    replaced with what looks like a padlock, which I have called
    sallylock_mc (it is a movie clip, don't know if it has to be or if
    it should be something else.) Also, I want the padlock to only
    exist/be visible on the "main" scene. I've found that when I go to
    the other scenes, the padlock is there, ontop of everything else.
    I've attached the code in "main", and in "sally":

  • Trying to remove movie clip with collision?

    I am creating empty movie clips and populating them with
    movie clips that have "drag and drop" functionality. I can create
    the empty movie clip and populate it with an external .swf. I've
    even got the drag-n-drop handled. But, I'd like to have them delete
    or disappear when they are dropped onto a trash can movie clip. I
    just can't seem to figure out the code for this. I would greatly
    appreciate any help you could give me.
    Thanks.

    you need to perform a hitTest to determine if the item is
    within the bounds of your trashcan, you can also use _droptarget,
    but I prefer hitTest with a shapeflag parameter. depending on your
    code, when you call stopdrag onRelease, run your test there.
    the_mc.onRelease = function() {
    stopdrag();
    if(_level0.trashcan.hitTest(_xmouse, _ymouse, true)) {
    this.removeMovieClip();
    }

  • Unable to Remove Movie Clip

    Hello... I am an ActionScript newbie. I followed a tutorial
    from computer arts magazine, to create a portfolio using
    actionscript and xml. I linked the portfolio to several scenes in
    flash, such as contents and contact details. My problem is that
    when I placed a button "back" on the main stage of the portfolio,
    to redirect to the contents page, the list of artworks thats
    populated from xml remains on the next scene, or any other scene i
    link it to. I tried using unload movie clip, but nothing is
    working. Please Help. This is my actionscript:
    I don't if this helps, but I get this message in the output
    panel: Target not found: Target="_root.btn_projectsundefined"
    Base="_level0"

    check the flash help files. there's no better way to debug
    your flash applications than the liberal use of the trace()
    function.
    for example:

  • Movie Clips in Array

    I am trying ot push attached Movieclips into an Array
    btnUp.onRelease = function():Void {
    var grp1:MovieClip = test.attachMovie("Group1", "mcOne", 1);
    myarray.push(grp1);
    I think this works.
    What I am trying to do is that after the user select the
    movieclips he or she wants (40 choices total) they will be pushed
    into an array and that when they select finished choosing the
    movieclips will appear in order in which they chose them in another
    frame or movieclip. But when it comes to arrays I am
    clueless.

    the code to display the selected movieclips (in order) is
    after the comment.
    when you're ready to start the display, set index=0 and use
    that code after the comment.
    to have those movieclips appear at a designated _x,_y assign
    myarray[index]._x and myarray[index]._y to suit your needs.

  • Cant remove movie clip in preloader

    For some reason the mc is not removed from the stage when my preloader is finished loading ...
    function Preloader(event:ProgressEvent):void {
         var mc:preloader = new preloader();
         var percent:Number=Math.round(event.bytesLoaded/event.bytesTotal*100);
         addChild(mc);
         if (percent==100) {
              removeChild(mc);
    xml_loader.removeEventListener(ProgressEvent.PROGRESS, Preloader);
    Am I missing something here?

    yes.
    each time Preloader() is called you're creating a new preloader instance (all with the same reference, mc).  so, if you call Preloader() 25 times, you're adding 25 instances of mc and, on the final call to Preloader(), you remove one mc instance and stop the calls to Preloader().
    to remedy, use:
    var mc:preloader=new preloader();
    addChild(mc);
    function Preloader(event:ProgressEvent):void {
         var  percent:Number=Math.round(event.bytesLoaded/event.bytesTotal*100);
    // do something with mc to reflect percent
         if (percent==100) {
              removeChild(mc);
    xml_loader.removeEventListener(ProgressEvent.PROGRESS,  Preloader);

  • Removing movie clips

    I have this bit of code all by itself in a frame on the main
    timeline
    square.removeChild(squarechild);
    it works great if squarechild exists. but if squarechild
    doesn't exist the main timeline just stops. In AS3 how do you say
    "if squarechild exists, remove it, if it doesn't, just keep going"?
    Thanks for your help

    If you look just a little ways down the forum page, you'll
    find some info that may help. Here's the link in case it's not
    obvious:
    http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=665&threadid =1428651&enterthread=y

  • Removing Movieclips from an Array Displayed on stage

    Hi Everyone ,
    I am new to AS3 so please forgive me in advance if I end up annoying anyone ;(
    I need some help in removing Movie Clips from the stage , these were initially loaded off an array
    Here is the Code with the NEXT Button , : for both ARRAYS , Notes and Notes15 ( referring to 15th frame)
    function nextframepop2(Event:MouseEvent):void{
    trace("Mouse Enabled");
    removeChild(notes15[4]);
    removeChild(notes15[3]);
    removeChild(notes15[2]);
    removeChild(notes15[1]);
    removeChild(notes15[0]);
    nextFrame();
    function nextframepop1(Event:MouseEvent):void{
    trace("Mouse Enabled");
    removeChild(notes[1]); // This is Line 635 , but I am not sure what I am doing wrong here.
    removeChild(notes[0]);
    nextFrame();
    I am calling this Function with an If Statement , I need to make sure all the Movie Clips are loaded before the End User decides to move to next slide.
    Here is what I get when I run the Next Button ,
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at PORTDemoPresent_fla::MainTimeline/nextframepop1()[PORTDemoPresent_fla.MainTimeline::frame 2:635]
    Can anyone please help ?

    Based on what you show it is likely that some other function (like the one before it as shown) has already removed the object.  What you should do is learn to use the trace function to troubleshoot your code.  In this case put in a trace anywhere you add or remove the same object that tells you whether it is added or removed and which line of code it is, realizing that you might be storing the same object with different references to it.  That way you should be able to isolate where things are being removed when you don't expect them to be.

Maybe you are looking for