Drawing tile engine fast with Flash (Actionscript 3)

Started by
5 comments, last by sebbit 15 years, 2 months ago
I have made a tile engine in Flash (Actionscript 3), but I'm not sure if my method is actually the fastest method available! I use pure actionscript 3 (with mxmlc compiler). The way to get an image is like this:
[Embed(source="layer1/tile0.png")] private var Tile0:Class;

And then to draw it, you need to do:
tile0 = new Tile0;
tile0.x = x;
tile0.y = y;
displayObjectContainer.addChild(tile0);

To "undraw" it, you need to do:
displayObjectContainer.removeChild(tile0);

This to draw only 1 of the tiles currently in view. In the actual engine, I do "new c", where c is the embedded image class of the current tile (in the example above "Tile0" is hardcoded but in reality it's dynamic). So for every tile in view, every frame, I have to use "new" to create a new object, do addChild, and, remember it in an array so that when drawing the next frame, I can do "removeChild" on everything first (unlike what I'm used to with SDL and OpenGL, where you don't have to "undraw" anything, you just draw new stuff over the old frame). I'm wondering if doing "new", "addChild" and "removeChild" per frame is efficient, or if there are better ways to do this in flash? Every frame, your location can change and individual tiles in the world can also change (e.g. water becoming something different), so I don't see an easy way to do "addChild" only once per tile and updating x and y locations every frame. Thanks! [Edited by - Lode on January 25, 2009 7:51:15 AM]
Advertisement
This adds *a lot* of overhead as Flash is creating new objects for each tile in your map. Then you're using the built-in screen manager to handle the positioning and redrawing of each tile.

It is much faster to use the same double-buffering, with the copypixels method, that you're using to with SDL/OpenGL.

You can find some benchmarks of Copypixels vs Sprite-Pool (your method) at http://board.flashkit.com/board/showthread.php?t=732354 if you want hard numbers. Copypixels beats the pants off of the generic Sprite or Bitmap objects.


This looks interesting. Does anyone know places with good tutorials on making an engine like that with AS3? Also, from the replies it doesn't look easy to have an alpha channel with the copypixel method? (which I need)
You can use direct-pixel stuff for absolute top performance, but your tile-based approach can be much quicker without such drastic changes.

Exactly how is hard to say, since I don't know how each tile is changing each frame. But the copy-pixel route is almost certainly best if you want a game where the background is changing... scrolling backgrounds don't count, in many cases you can set up the whole level in advance.

www.simulatedmedicine.com - medical simulation software

Looking to find experienced Ogre & shader developers/artists. PM me or contact through website with a contact email address if interested.

I'd like the tiles to scroll smoothly if you move. Can the copypixel method be combined with tweens for this scrolling, or are there other better scrolling methods?
Just move the entire container...
If you are still interested in tutorials about copypixels/blitting this could be helpfull.

This topic is closed to new replies.

Advertisement