Skip to main content
Inspiring
September 23, 2011
Answered

Need help removing a bunch of objects added at runtime via a timer

  • September 23, 2011
  • 1 reply
  • 796 views

Hi.  After adding an image to the stage, turning it into a Movie clip, and setting linkage in library to Export for Actionscript, I'm now using a timer to add a bunch of roses to the stage as a video plays.

How can I get rid of all these clips once the video is over?

Do I need to add all of them into an extra container and then just removeChild that container at the end?

Or do I need to use something like getNumberOfChildren and then create a loop that removes them one by one?

The clips (myRose) are fairly small and do not have any event listeners added to them.  I just use TweenMax to move them from top to bottom of the screen.

I've pasted my code below.

Thanks for any suggestions.

public function roseTimer():void

        {

            MonsterDebugger.trace(this, "in roseTimer");

            myRoseTimer = new Timer(1200, 52);

            myRoseTimer.addEventListener(TimerEvent.TIMER, createRose);

            myRoseTimer.start();

        }

       

private function createRose(event:TimerEvent):void

        {

            MonsterDebugger.trace(this, "test create rose see if stop blinking one");

            MonsterDebugger.trace(this, "in createRose");

            var myRose:rose = new rose();

            var startX:int = (stage.stageWidth/8) + (Math.round (Math.random() * (stage.stageWidth*.75) ) );

            var makeSubtract:int;

            //Math.random()>.5 ? makeSubtract = 1 : makeSubtract = -1; //shorthand if statement

            var endX:int = startX + Math.random()*100*makeSubtract;

            if (endX<100)

            {

                endX +=100;

            }

            if (endX> stage.stageWidth -100)

            {

                endX -=100;

            }

            //MonsterDebugger.trace(this, "makeSubtract = " + makeSubtract + " and startX = " + startX + " and endX = " + endX + " and stage.stageWidth = " + stage.stageWidth);

           

            //var myScale:Number = 1 + (Math.random()*.3*makeSubtract);

           

            var startY:int = Math.round(Math.random()*100);

            var endY:int = stage.stageHeight - Math.round(Math.random()*175);

            var startRotation = Math.random()*360;

            var endRotation = Math.random()*360;

            myRose.x = startX;

            myRose.y = startY - 150;

           

            //TweenMax.fromTo(myRose, 5, {scaleX: myScale, scaleY: myScale, x:startX, y:startY, alpha: .4, rotationZ:endRotation}, {scaleX: myScale, scaleY: myScale, x:endX, y:endY, alpha:.9, rotationZ:endRotation});

            TweenMax.fromTo(myRose, 5, {x:startX, y:startY, alpha: 0, rotationZ:startRotation}, {x:endX, y:endY-75, alpha:.9, rotationZ:endRotation});

            //myRose.visible = true;

            //Gaia.api.getDepthContainer(Gaia.TOP).addChild(myRose);

            addChild(myRose);

        }

This topic has been closed for replies.
Correct answer Dave_Wolfe

Adding them to their own container will work, or you can store a reference to all your movie clips by putting them in an array, and loop through the array when you want to remove the children. 

1 reply

Dave_WolfeCorrect answer
Inspiring
September 23, 2011

Adding them to their own container will work, or you can store a reference to all your movie clips by putting them in an array, and loop through the array when you want to remove the children. 

reindeer4Author
Inspiring
September 23, 2011

Hey Dave -

If I add them to a container and simply removeChild the container, will that avoid any memory leaks/problems?

Wasn't sure if that was just an issue when eventListeners were involved.

Thanks

Inspiring
September 23, 2011

As long as there are no other references then you shouldn't have any memory leaks.