ActionScript 3: Why is it not removing the movieclips?

Started by
4 comments, last by Felipe M 11 years ago

Hey. Didn't know if it was better to post here or "Tools and APIs" section, anyways.

I have my little game where there are some enemies falling from the sky. There are multiple enemies on the screen at once, so I keep them in an array. The enemies are movie clips and they have a class associated to them that Adobe Flash created for me. So when they are no longer in the screen I do this:


stage.removeChild(enemies[0]);
enemies.shift();

Where "enemies" is my array of enemies. And I always know that the enemy out of the screen is the one in the first position of the array.
The game runs smoothly but then starts lagging when more enemies are spawned, so I have a high suspicion that the enemies are not actually being removed from memory. Why is it happening?
Thanks already.
Advertisement

Assuming the movie clips are being properly removed from the stage, my guess is that the garbage collector isn't running because you haven't actually used enough memory for it to think that collection should be performed, however I'm not sure that this would be cause any lag over time.

There are ways you can force the garbage collector to run, but another approach you can try first is to reuse your enemy objects. So whenever an enemy is destroyed you move it into an 'unusedEnemies' array, and whenever a new enemy needs to be spawned you try to reuse an object from this array and reinitialize its data as opposed to new-ing enemies every time.

Are these enemies animated ? Removing a enemy from the stage, doesn't stop the movieclip from animating. So if you have enemies that are animated via tweens, they are likely still doing so if you haven't stopped the animation. That would explain the lagging over time.

Do the enemies have any event listeners attached to them ? That might be a reason why they are not being garbage collected.

But it's easy enough to test my theory if you have animated enemies, by just stopping the animation when you remove. We are theorizing at this point that they are not being removed. (good theory)

I did some tests and found out that it's maybe not the enemies that are lagging over time. I still don't know what is causing this issue, but I've tried to to comment the content of the function that I use to spawn enemies, where I construct and put them in the array, and the game still lags, without any enemy spawning.

Assuming the movie clips are being properly removed from the stage, my guess is that the garbage collector isn't running because you haven't actually used enough memory for it to think that collection should be performed, however I'm not sure that this would be cause any lag over time.

I don't know how a garbage collector works, but I did this:


stage.removeChild(enemies[0]);
enemies.shift();
System.gc();

The game still ran but the lag over time problem was still there.

Are these enemies animated ? Removing a enemy from the stage, doesn't stop the movieclip from animating. So if you have enemies that are animated via tweens, they are likely still doing so if you haven't stopped the animation. That would explain the lagging over time.

Do the enemies have any event listeners attached to them ? That might be a reason why they are not being garbage collected.

But it's easy enough to test my theory if you have animated enemies, by just stopping the animation when you remove. We are theorizing at this point that they are not being removed. (good theory)

For now my enemies just have two frames, each with a stop(); on the frame. I just use this to change their image when they are damaged. About if they have a listener, I don't think so, at least not that I know. I only declared these three for now:



stage.addEventListener(Event.ENTER_FRAME, mainLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveB);
stage.addEventListener(KeyboardEvent.KEY_DOWN, changeLaneCount);

Anyway, in my code I have commented all the lines that I thought that could possibly make my game lag over time, no sucess. I'm kinda stuck now. Do you have some tips on how to solve this kind of problem?

So you have no enemies spawning, and only a ship or something ? What graphical elements are active ?

Is there a scrolling background ?

here's a performance stats monitor that can possibly give you a hint as to what is going on :

https://github.com/MindScriptAct/Advanced-hi-res-stats

I'm assuming if you are experiencing lagging, you'll see the framerate drop, but take note of your memory consumption as well to see if it's fluctuating large chunks.

Also, do you have anything text based on your screen ? Large amounts of text on the screen can hurt performance.

oh.. last one.. Are you tracing any output ? If you are tracing things constantly, that can cause a lag as well.

If you have a background, is it bitmapCached ?

Something like this is really hard to troubleshoot without knowing more about what's going on in your code. I'd try to isolate it by disabling aspects until you see the problem go away.

Sorry for not answering, but I'm full of college work to do and haven't had time to see this thing anymore. I'm postponnig this game for now, when I have time again I'll see again and see what you proposed prototypical. Thanks anyway.

This topic is closed to new replies.

Advertisement