Using haXe for Platform Games
In 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!
February 26th, 2007 at 10:32 pm
Big fan! I remember way back when you created the Mario clone for DOS.
I just wanted to send you a link to another scrolling engine that you might be interested in:
http://www.strille.net/works/misc/platform_demo/
It should be somewhat easy to transfer to Actionscript 2. I haven’t used Haxe yet, so I don’t know if they are using Actionscript 2 or 3. The only open source actionscript compiler I’ve used so far is MTASC.
Don’t stop posting. I’m enjoying your site.
May 19th, 2007 at 7:47 pm
I’ve never programmed in Flash before (other than OpenLaszlo), and I just found haXe a few days ago. It’s hard to find examples but I found a Aereo Fighter clone with source at http://www.cairnsgames.co.za/files/flashshooter.htm and have started using it to make a simple platformer. The first technique you mentioned of just swapping MovieClips is what I’m using for the player animation like switching directions. I’m curious about the other methods. A small snippet demonstarting how to GET BitmapData and how to bitblip it would be much appreciated, because I have no experience in ActionScript and no idea how to get BitmapData or where to get it from or how to bitblip it to a MovieClip. I’m just using a swiffmill resource swf and attaching those bitmaps to MoiveClips then swaping ‘em.
May 21st, 2007 at 10:45 pm
WOW! Awesome! I’d be really interesting in knowing how you can use BitmapData in haXe.
June 24th, 2007 at 8:43 am
OK, I looked at the haxe example of a flash scroller for a good half an hour now, and i really don’t get it
, where do you specify that the pink is the transparent color in order to eliminate it in the resulting swf ?
June 24th, 2007 at 8:56 am
OK, sorry, you export PNG’s as RGBA
, my viewer (XnView) displayed the pink fully opaque, although it has alpha 0.
But then, i have another question, in Tile Studio, whenever i import png’s created by it, with some transparent color, the transparent color is taken as white, which is a problem since when i set it to white, it makes also opaque white as a transparent color, and since white is a common color i get unwanted bitmap regions as transparent. Is there a workaround, or is it a bug?
July 25th, 2007 at 1:45 pm
First, thanks for Tile Studio, great piece of software, the code exporter is very good.
I’m currently working on a game engine around TileStudio exports in pure Actionscript 3. Performance is the main concern. It currently scrolls a 3000×3000 single static layer
January 13th, 2008 at 9:15 pm
Love your site. I’m pretty new to haxe and I haven’t had much luck finding a platform game source.Your other scrolling example was nice but being new I don’t think I can get a platform game going off just that. I don’t want to be pushy but could you release the source to this Mario game aswell. I think I’d as well as others couldlearn a great deal from it.
Thank you for your time.
JJ
April 29th, 2008 at 10:08 am
hehe. Funny how you apply console programming hacks (wrapping surface with their offscreen part replaced with new tiles) on a flash player that has actually no hardware to hack ^_^
May 14th, 2008 at 4:09 pm
I have been looking for months it seems on how to do a bitmap scroller like what you have provided… everything else is creating a huge bitmap (over 1000 pixels wide at least), yet you are able to accomplish it using only ~400 pixels!
By chance would you please write a tutorial on how you did this? Also, if possible, can you write it without using haXe? I already am trying to struggle to understand the basics of AS3, and trying to develop what you just did with two different languages won’t be an easy task :/
Again, I, and so would thousands of others, would really love it if you could write a tut on how you did this, thanks in advance
August 1st, 2008 at 10:27 pm
anybody here know of a good site to find more info on mario flash game? I\’ve got this site bookmarked and im gonna keep checking it out, but i still would like to find a site that covers mario flash game a little more thoroughly..thanks