Using haXe for Platform Games
January 30th, 2007In the past weeks I’ve been experimenting a lot with haXe, a new programming language that can compile to Flash SWF files. Last summer, I already played around with haXe for a while and made a little flash scroll demo. Recently, I’ve been trying out different methods of drawing tiles, in order to find the fastest method.
At first, the difference in speed appeared to be huge, but those were rather the result of other optimizations I did in the meantime. All these methods work at roughly the same speed if you’re using large tiles. But when you have small tiles (which I normally use for games) and you have hundreds of them on screen all the time, handling all those tiles seems to become far slower than rendering them.
Here are three methods I tried:
- The first is the method used in the scroll demo. Every tile is represented by a separate MovieClip, the total number of MovieClips stays constant, when a tile goes off-screen, its MovieClip is reused for the tile that appears on the other side. Using this method makes it very easy to add animated tiles, since they are handled by flash.
- The second method is the same method as above, but with only one MovieClip with a BitmapData attached to it (as the screen surface) and just “blitting” all the tiles to it using copyPixels. Here you need to implement tile animations yourself.
- For the third method (demonstrated below), each layer has its own BitmapData object as a surface (size of the screen + 2 tiles), which scrolls along with the layer. That way, you only need to redraw the tiles that actually change (at the edges and animating tiles). When you start to scroll, you’ll run out of space on the surface, but it wraps around to the other side. So normally, the screen will be divided in to sections horizontally and vertically. To get all those back together, there are 4 MovieClips per layer and the BitmapData is attached to all 4 of them. By placing them next to each other, you get the entire screen in the middle. The Mario example only scrolls horizontally, so it only uses 2 of those surfaces (per layer).
This little demo is based on my Mario clone for DOS, there are no enemies and the only thing you can do is collect coins. If you are using FireFox, click on the game first. Use the arrow keys to walk and space to jump. Enjoy!