A tutorial that shows how organize graphics and sprites in a fighting game, how to implement the controls and manage the various moves.
Making a Beat-Em-Up Fighting Game in Flash: Part THREE
Most of the beat-em up tutorials out there on the net will show you can make a character walk around and maybe slug out a few punches, and yet most never go any further, but what we need is some tighter code that will handle such things as detecting whether or not the user has the attack key held down, and a routine to stop Rick moving whilst attacking.
keydown is used to check the attack key has been released before it can be pressed again. Without it Rick would repeatedly attack as long as you have the attack key held down, and that's something no good fighting game should allow (though you will find thousands of second-rate Flash games out there that do).
Here's the rest of the code for attacking. I'm not going to bother chopping it up into titbits, I'll just explain it piece by piece below.
if (Key.isDown (Key.CONTROL) && keydown==false)
{
rick_mc.attack=true;
}
if(rick_mc.attack)
{
if (Key.isDown (Key.DOWN))
{
rick_mc.gotoAndStop("crouchkick");
}
else
{
rick_mc.gotoAndStop("punch");
}
}
if (Key.isDown (Key.CONTROL))
{
keydown=true;
}
else
keydown=false;
DON'T RUN THE CODE YET!
We still have to do something for it to work properly. But first, an analysis. How does the snippet work?
To start, we only allow the user to enter the attack state if the attack key (CTRL)has been released. If so, we set attack to true. In the attack loop we simply determine which moves were executed. Note; the routine is made for ground moves only- as later on we will create another one for jump attacks.
Finally, we check to see if the CTRL key is down. The code works effectively only because it is below the attack==true routine. Even though you have the CTRL key held down, you can still move Rick around, just like in the original game.
So, what's that thing I mentioned that we needed to do. We've managed to set attack to true and get our Rick punching and kicking, but somewhere the attack needs to be set to false, or else he will never stop the unending physical onslaught (a bit like my wife). As some of the smarter folks out there may have guessed, the attack needs to be set to false at the end of every animation. Now, there are a million ways to do this, such as using _totalframes to check when each animation has reached it's end, but a simpler way is just to stick a tiny bit of actionscript on the final frame at the end of each attack. Yes, experienced coders are probably waving their fingers and muttering things such as "You should always keep your script on one frame", but screw 'em. So, let's start with the punch. On the main stage, double click on Rick, then go onto the 'punching' frame, and double click him once again. Click on last frame within the grey ones in the timeline, and tap F7 to Blank Keyframe it. Then, go into Actions, and enter the following:
_parent.attack=false;
_parent is a command that sends code up a level. Since we're actually in _root.rick_mc.some-yet-undefined-timeline , and the attack variable is actually in _root.rick_mc, we have to do this. Even if you don't understand what the hell I'm going on about, just do it anyway.