Flash Game Development with Haxe: A Simple Snake Game

I’ve been meaning to pick up the language haxe and I’ve also been meaning to do some flash game development as well. Two birds, one stone and all that jazz means that over the course of the next week I’ll put together a simple flash snake game using haxe. Nothing fancy here, just me exploring flash game development in haxe and sharing my experience as well as explaining my code. You can download the code to this article here. Let’s get started then.

My goal for this first day is just to move a square around based on key presses. In InputManager.hx you’ll find a simple input manager. Skipping past all the key code defines you’ll find this:

private var key_bindings : IntHash<Dynamic>;
 
public function new()
{
    key_bindings = new IntHash<Dynamic>();
}

What is going on here is a hash table mapping Int keys to variables of Dynamic type. Dynamic can be basically anything in Haxe and in this case we are storing functions and the Int keys are keycodess.

public function bind(key : Int, func : Dynamic)
{
    key_bindings.set(key, func);
}

The bind function simply binds a keycode to a function.

public function handleInput()
{
	for(key in key_bindings.keys())
	{
		if(key > 1000)
		{
			if(flash.Key.isDown(key-1000) && flash.Key.isDown(SPCL_KEY_Shift))
			{
				key_bindings.get(key)();
			}
		}
		else
		{
			if(flash.Key.isDown(key))
			{
				key_bindings.get(key)();
			}
		}
	}
}

The meat of the InputManager class the handleInput function iterates over our keys and if the key is down it calls the corresponding function we bound to that key. Flash doesn’t distinguish between upper and lower case presses so I got around that by adding a constant (1000) to the key code for those which require the shift key to be down and can detect those key presses separately.

The main class in SnakeGame.hx is pretty simple so I’ll only cover the one interest bit there:

var binder = function(obj, arg){ return function(){ obj(arg); } };
 
_input = new InputManager();
_input.bind(InputManager.SPCL_KEY_LeftArrow, binder(_snake.move_left, 10));
_input.bind(InputManager.SPCL_KEY_RightArrow, binder(_snake.move_right, 10));
_input.bind(InputManager.SPCL_KEY_UpArrow, binder(_snake.move_up, 10));
_input.bind(InputManager.SPCL_KEY_DownArrow, binder(_snake.move_down, 10));

Some of you may be scratching your heads at that bit of code. What is going on is this: we create a variable binder which is a function taking two arguments (an object method and an argument). Binder itself returns a function which calls the object method (or any function) with the argument provided. In doing so we can take a method like move_left in the snake class which takes an Int argument and pass it in to the InputManager as a function which takes no arguments. Simply put when we press the left arrow the InputManager calls a function whose body contains _snake.move_left(10). That’s it. Tune in next time when I give this baby a tail.

Here’s the result (click on it to activate):


Leave a Reply