Saturday, August 14, 2010

How to code a Beat em up

Some of you may remember a long time ago I did write a tutorial on beat em up's, but it was in AS2, and had a few major flaws I picked up on, therefore it was removed.
I figure now is the time to update those tutorials in AS3. This isn't going to be a straightforward making-from-scratch tutorial, rather a guide to the techniques involved. You'll have to do the grunt work yourself- no easy download FLA's for the lazybones here!
-------------------------------------------------
ADVANCED KEY-CAPTURE
-------------------------------------------------

Second only to a real fighting game such as Street Fighter 2, I can't imagine which type of game requires more pinpoint, tight arcade controls than a beat em up. Besides knowing exactly when a key is pressed, after we know what key has been pressed, we will also want to know when it was last pressed (in milliseconds), if it is being held down, and if so for how long.

//////////KEYS
var KEY_UP = Keyboard.UP;

var KEY_DOWN =Keyboard.DOWN;
var KEY_LEFT = Keyboard.LEFT;
var KEY_RIGHT = Keyboard.RIGHT;
var KEY_ATTACK = 87;
var KEY_JUMP = 81;
/////////////////////////////////////////////////////////////////////


First we need to set up some constant variables for the key-codes. As you can see, with the directional controls I'm just using the arrow keys therefore I can use the globals Keyboard.UP etc. KeyCode 87 = "W", KeyCode 81 = "Q".

var keys = new Object();
keys[KEY_UP]={down:false,count:0,held_down:false,timerA:200,timerB:0,timeElapsed:0};
keys[KEY_DOWN]={down:false,count:0,held_down:false, timerA:200,timerB:0,timeElapsed:0};

keys[KEY_LEFT] = {down:false, count:0, held_down:false, timerA:200,timerB:0,timeElapsed:0};

keys[KEY_RIGHT] = {down:false, count:0, held_down:false, timerA:200,timerB:0,timeElapsed:0};

keys[KEY_ATTACK] = {down:false, count:0, held_down:false, timerA:200,timerB:0,timeElapsed:0};

keys[KEY_JUMP] = {down:false, count:0, held_down:false, timerA:200,timerB:0,timeElapsed:0};

Next I've made an Object that contains information on all keys used.
down = Is the key being pressed?
count= How many times has the key listener detected this key being held down?
held_down= er..is actually being held down?
timerA and timer B are used for checking the length of time it's being held down.
timeElapsed= might or might now be useful till later.


stage.addEventListener (KeyboardEvent.KEY_DOWN, downKeys);
stage.addEventListener (KeyboardEvent.KEY_UP, upKeys);

Our keyListeners. What is a listener? Think of it as a function that never stops working until you tell it to. In other words, remove it.




function downKeys (ev:KeyboardEvent)
{
if (keys[ev.keyCode] != undefined)
{
   keys[ev.keyCode].down = true;
   keys[ev.keyCode].count++;
   keys[ev.keyCode].timerA = getTimer() - keys[ev.keyCode].timerB;
   keys[ev.keyCode].timerB = getTimer();
}
};

function upKeys (ev:KeyboardEvent)
{
if (keys[ev.keyCode] != undefined)
{
   keys[ev.keyCode].down = false;
   keys[ev.keyCode].count=0;
   keys[ev.keyCode].held_down=false;
}
};

////////////////////////////////////////////////////////////////



downKeys listens for pressed keys, and upKeys listens for unpressed keys.
Okay so every listener triggers an event. Don't worry too much about what that means, anyway the ev:KeyboardEvent part means 'ev' equals the key that has been picked up by the listener. We can use it as a reference into our handy keys object.

if (keys[ev.keyCode] != undefined)

Only proceed if a key has been picked up by the listener.

keys[ev.keyCode].down = true;

This key has been pressed, so set down to true.

keys[ev.keyCode].count++;

Increment variable count- telling us how many times the listener has picked up this key being held. *NOTE* This doesn't work quite as simply as you might think, the AS3 Key Listener is a funny beast- to be explained later.
keys[ev.keyCode].timerA = getTimer() - keys[ev.keyCode].timerB;

getTimer() returns the number of seconds since the movie has been playing. TimerA is the now time. The old time - the timer value it gives us right now. Think about it, you'll get it!

keys[ev.keyCode].timerB = getTimer();
Set timerB, the then time.


So, now I can detect whether a key is being pressed with a line like:

if (keys[KEY_JUMP].down)
{
trace("JUMP has been pressed");
}

This replaces the old AS2 isKeyDown nicely. In fact, it's better isn't it? Not only can we check if the key is down, but we can also get more information about that key.

p.s.Don't forget your libraries at the top.:

import flash.events.KeyboardEvent;

import flash.events.Event;

Monday, August 02, 2010

'INSANITY II' bidding on FGL

So the game is done, completed 100% and on Flash Game License now awaiting bids. I am in no rush at all to sell, in fact I've half a mind to pimp it out armed only with mochi or cpmstar ads.
I figure if 'THE INSANITY' hit over 1,000,000 players in a half year, that would effectively have gotten me about $1000 in ad revenue by now- and if I could be patient for a few years, that could dd up and add up, who knows? I'd have the cash the sponsors paid for it minus any legal obligation to put someones logo on my game. So we'll see, the only stress for me is the fans out there won't get to see the game till it's been through the bid wars. I'm still adding bits and pieces to the game while it's up there waiting for a pappa.
In the meantime, work starts on the next project. It's time for ACCCCCTTTTIIION! Yes, enough of these dour creepy-crawlie point'n'c*lickers EvilKris is breaking from tradition. Although keeping with the horror theme would make for sound business sense, I'd also like the kiddies to be able to play my games occasionally without their mum's going 'What the bleedin' hell you playing here?- that's bloody disgusting turn it off!'
So the next game will be using my Streets Of Rage Fighter Engine (rewritten in AS3) and will be a simple game kinda similar to the old classic MoonStone albeit shorter and much more hyper- paced. Essentially you'll have a map with about 50 random locations you can 'search', hit any spot and you'll have some enemies to fight and on winning you'll either get a weapon, something to give health recovery, or a hint as to where the final location you're looking for is.
I haven't quite decided on the theme yet but it'll probably be something with barbarians and goblins as I've been playing Oblivion lately. Had a dream I was working for Bethesda the other day.
Tell you what it feels damn nice to get off Insanity 2, summer is here, the babes are on the beach, and my own complication in life is figuring how to get the best suntan.