AS3 Scroller

This is a demo I wrote to experiment with scrolling graphics for creating a tile based game. Use the arrow keys to move up, down, left or right. Frames per second are displayed to measure performance - the stage is set to 120 frames per second so that should be the highest number possible (most browsers these days won't let you go above 60 FPS.

The map is stored in an array of integers representing which tiles to draw (0 = grass, 1 = tropical tree on top of grass, 2 = deciduous tree on top of grass). The map is randomly generated but it is a real map, if you move back and forth you can see the same trees you just passed scroll back on screen.

I used to write 2D games back in the 90's when computers were much slower, so I am used to pushing CPU's to the limit. One of the techniques I like to use for scrolling games is instead of redrawing the entire screen each frame, I play around with the coordinate system so that I only have to draw new tiles that are about to scroll on screen.

In AS3, it seems the best way to handle this is to break the screen down into tiles (in this demo a tile is 50x50 pixels). Each tile is stored as a Bitmap on screen, and stored in an array. Whenever the screen is scrolled, all that is needed is to cycle through all the tiles and change the x or y coordinate, rather than redraw everything. Before scrolling I also draw the new tiles that are coming onto the screen. As tiles are pushed off screen, they are removed from the stage so that they no longer take up any memory or resources.

I'd like to experiment more with scrolling to see if I can get better results, however I have yet to see this demo choke. When I set the demo to 1000x800 pixels (the current size is 600x450), I was still getting over 40 frames per second.

Since I've written this demo, I've noticed I can get even better performance by putting all of the graphics into a parent Sprite, and moving just one Sprite to scroll. Reusing Bitmaps (instead of deleting and creating them) also improves performance, as garbage collection is usually the bottleneck for performance in scrolling games.