How to delete MovieClips?

18 posts

Flag Post

I am making a game and it has ammo and wind and whatnot and each bullet has it’s own wind variable, so if the wind is to the right, and you shoot to the left, it will eventually come back regardless of what the wind is now. So anyways, how do I delete each bullet when it leaves the screen?

 
Flag Post

1) remove any event listeners you added to the movieclip.
2) call removeChild() to remove the object from the stage
3) delete all the references so the garbage collector can free up the memory the movieclip used.

 
Flag Post

I’m getting the error

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at main/loop()

 
Flag Post

AS2? bullet.removeMovieClip(). And yes, if you have external event handlers (ones within the class itself are ok), you need to detach them, and remove all references to the object in your code (for example an array containing all bullets) before it will get garbage collected.

 
Flag Post

Why won’t this work? It is very simple

main.as
http://codepad.org/PgAvRVnH

circle.as
http://codepad.org/2YhdhZxF

 
Flag Post

Codepad doesn’t work for me, I get an internal server error. Could you try uploading to pastebin?

 
Flag Post

main.as
http://pastebin.com/nmbNcjWq

circle.as
http://pastebin.com/ez2D6rgA

 
Flag Post

I think the problem is that, after removing an instance, the tick function inside that instance runs again, and tries to delete the child again.

So instead try this:

if(!alpha)
{
parent.removeChild(this);
removeEventListener(TimerEvent.TIMER, tick);
}

EDIT: That is in the tick function of Circle.as :)

 
Flag Post

Still not working, the program eventually slows down, and the memory keeps going up.

 
Flag Post

That may be due to the fact you try to create 1000 new circles every second? What error do you get?

 
Flag Post

The memory goes up like 1mb per 10 seconds. I have:

this.alpha -= 0.01;
if(alpha<0){
parent.removeChild(this);
timer.removeEventListener(TimerEvent.TIMER, tick);
}

EDIT: There is no error.

 
Flag Post

It’s logic, really. In your main class, you create a timer that fires every 1ms, that runs a function that creates a new circle:

var timer:Timer = new Timer(1);

The value that you give the timer function is the amount of milliseconds. If you want it to fire every second, type this instead:

var timer:Timer = new Timer(1000);

Creating 1000 circles every second equals 10.000 circles every 10 seconds. No wonder your memory goes up :)

As for the timer in the circle class:

var timer:Timer = new Timer(10);

It ticks every 10ms, meaning 100 times per second. You decrease alpha by 0.01 every second, meaning that after 1 second it’s down from 1 to 0, and the instance gets destroyed.

 
Flag Post

Yes which would mean there would be no more then 1000 at a time, but like I said, the memory keeps going up.

 
Flag Post

Are you sure the movieclips are actually being removed? Try putting a trace statement inside the if to see.

I suspect you’re running into floating-point errors.

 
Flag Post

I get no errors and yes I’ve tried the trace inside the if statement and it goes off.

 
Flag Post

Try tracing ‘this.numChildren’ every time you create a circle. (In the tick function of your main class) If it exceeds far past 1000, something’s wrong. If not, it’s likely that something’s keeping the garbage collector from collecting the removed circles..

 
Flag Post

Try throwing in a timer.stop(). I’m not sure if a running Timer can be garbage collected.

Edit: If that doesn’t work, try this.

package{
	import flash.display.Shape;
	import flash.events.Event;
	import flash.utils.getTimer;
	
	public class circle extends Shape {
		private var time:int = getTimer() + 1000;
		
		public function circle(){
			graphics.beginFill(0xFF00FF);
			graphics.drawCircle(mouseX,mouseY,20);
			graphics.endFill();
			addEventListener(Event.ENTER_FRAME,tick);
		}
		
		private function tick(temp:Event):void{
			this.alpha = (time - getTimer()) / 1000.0;
			if(alpha <= 0) {
				parent.removeChild(this);
				removeEventListener(Event.ENTER_FRAME, tick);
			}
		}
	}
}
 
Flag Post

Okay it works now, I was checking the memory from flash.exe from preview mode or whatever and it was going up, but when I just open it in flash player it stays about the same. Probably something to do with debugging or something. Sorry for this.