Preparing for Ludum Dare AS3 development
Looking forward to participating in LD27? If Flash is your tool of choice, but have not used it in a while, here are some things you might want to rehearse before the contest starts. There isn’t much time during the compo to start looking up the order of function arguments or trying to recall how the overall structure of your game is supposed to work, so I like to practice the most commonly used things before the start.
Creating a simple game class
From starting up Flash, how do you get the basic game loop going? One option is to just use Flash actions editor, but as the Flash editor tends to be a bit slow and is lacking in some features, my personal choice is to edit all code in Sublime Text 2 and use Flash only to preview the result.
You can pre-create a snippet to serve as a starting point. As long as you declare your snippet before the contest starts, you are allowed to use it. Here is my starting point that just displays some noise on the screen. To use it, you create a new AS3 project in Flash, then save it as say Game.fla and from properties set “Class” to “Game”. Then save the snippet below as Game.as and launch:
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.utils.*;
public class Game extends Sprite {
var backbufferBitmapData;
var frontbufferBitmapData;
var past;
function tick() {
var now = getTimer();
var elapsedSeconds = (now - past)*0.001;
past = now;
}
var pixels:Vector.<uint>;
function render() {
var i:uint = pixels.length; // declaring type here is a huge speedup
while (i--) pixels[i] = 0xff000000 + Math.random() * 255;
backbufferBitmapData.setVector(backbufferBitmapData.rect, pixels);
}
function flip() {
frontbufferBitmapData.copyPixels(
backbufferBitmapData,
new Rectangle(0, 0, backbufferBitmapData.width, backbufferBitmapData.height),
new Point(0, 0)
);
}
function refresh(evt) {
var start = getTimer();
render();
var renderTime = getTimer();
tick();
trace(renderTime - start, 'ms / render()', getTimer() - renderTime, 'ms / tick()');
flip();
}
public function Game() {
past = getTimer();
backbufferBitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
frontbufferBitmapData = backbufferBitmapData.clone();
addChild(new Bitmap(frontbufferBitmapData));
pixels = new Vector.<uint>(stage.stageWidth * stage.stageHeight, true);
addEventListener(Event.ENTER_FRAME, refresh);
}
}
}

Getting bitmap data into your game
Recall that in flash there is BitmapData that represents the raw pixels. Then it has to be wrapped in a Bitmap to actually display it on the screen.
One thing you almost certainly will want to do is to use an external editor like Gimp or Photoshop to create a graphic, then somehow get that to display in your game from code. Say you have cat.png in your game directory. The way to get it to appear is to first bring up the library in Flash (⌘-L on Mac), then drag the image to the library. Edit properties of the image, check “export for actionscript” and give the class a name.
Easiest thing to forget when now instantiating that class from code is that you have to pass it 0,0. So if you called your class Cat, to get the BitmapData instantiated you do
var cat = Cat(0, 0);
You can now access the image data from code. To check that it is working, you can also add it to the stage by doing the following as the last line in your constructor.
addChild(new Bitmap(cat));
Clearing a rectangle
Recall that fillRect exists and that in AS3 you pass in a Rectangle object as an argument instead of separate coordinates. However for the color, an integer is expected. Like so:
// Clear a 100x100 rectangle at 100,100 to red. backbufferBitmapData.fillRect(new Rectangle(100, 100, 100, 100), 0xffff0000);
Copying pixels from one bitmapdata to another
You call the copyPixels method on the bitmapdata that will be changed. Argument order: Source – rectangle – point.
backbufferBitmapData.copyPixels(cat, new Rectangle(100, 100, 100, 100), new Point(200, 100));
Using filters
Filters are an easy way to make something look impressive easily. After you have a filter instance, you use it to change the pixels in a bitmapdata object.
You have to remember the following things:
– How to get an instance of the filter
– How to get that filter to change the pixels
Remember to import flash.filters.* first.
blur = new BlurFilter(10, 10, 10); cat.applyFilter(cat, new Rectangle(0, 0, cat.width, cat.height), new Point(0, 0), blur);
Cat appears twice there, because applyFilter is called on the bitmapdata that the result is copied to and the first argument is the source of the data that is passed to the filter. In this case both the source and destination are the same.
Other filters are already included. You can find the list here. After this preparation, some further things to do would be to play past winning games and think about why they won, how they managed their time. Look at themes that are currently being voted on, try to brainstorm what kind of games you might make from those themes. Good luck!


