views:

432

answers:

4

I have a question about loops in flash....

In a tile game I'm making a have a mini map of the whole level. The way it renders the map is a function with a for loop in another for loop. It cycles through each tile position and attaches a map piece (basically a 3x3 pixel square) which is colored according to what the tile is. Anyway, my problem is that when the level gets big like 50x50 tiles the map redering takes forever (up to 3 seconds). Is there anyway to fix this? Or is there another way of doing a mini-map?? If the level gets ever bigger it could take like 10 seconds!

Any help is appreciated!

Thanks, Matt

A: 

It shouldn't take long for the loops themselves to run, but what, exactly, are you doing inside each loop? What are the operations for attaching and coloring a map piece?

Adam Davis
I agree, it will be slow drawing that many objects but 3 seconds is a bit over the top.
grapefrukt
+3  A: 

Flash doesn't render tiles very fast. It's great at storing graphics that don't change in a buffer and quickly displaying this buffer quickly. Every graphics object (or sprite) that is added as a child to the stage has to be rendered independently.

Your problem is flash has to draw 50x50 = 2500 tiles every frame! Even if they 3x3 pixels, flash still treats them as separate objects.

It would be nice if you could store everything in a buffer or in one object and display it. So try drawing these 3x3 tiles in the same object instead of multiple objects. However this might hinder your functionality as the whole object will have to be re-rendered every frame.

Some other suggestions might be to render the objects in larger cells on a grid. Instead of having 50x50, start with 10x10 and then draw 5x5 cells in each cell. This would probably speed things up.

Hope this helps. If you find a good solution please post!

+1  A: 

Yes. The loop itself does not take long at all. But in each iteration I'm attaching a movieClip from the library. By the way it's not doing this on enterFrame, just when the user presses Pause (space).

+1  A: 

Most likely it's (as mentioned here) your loop that's slow. Counting 0-2500 is really fast but if you're doing heavy calculations in each iteration it will add up. Of course without seeing the code we can't help you with this. While you could do tricks to get that same loop run smoother (such as running it over multiple frames) but if it's a 3sec loop there's probably a lot you can do to optimize it – maybe even to the extent where it will run smooth enough, so that it won't cause a too long hickup.

Antti