Beat-em up fighting tutorial (updated) Page 3/5 Previous page Next page
[ January 21, 2007 ] Kris Foxton
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.

Let's introduce a new variable, keydown.

step=15;
attack = false;
keydown = false;
stance= 1;

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;

timeline

_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.

The complete code, squire.

rick_mc.step=15;
rick_mc.attack = false;
rick_mc.stance= 1;  
rick_mc.crouch=false;
keydown=false;

this.onEnterFrame = function()
{
	if(rick_mc.attack==false)
	{
		if (Key.isDown (Key.RIGHT) )
		{
			rick_mc._xscale = 100;
			rick_mc.stance=1;
			rick_mc._x+=rick_mc.step;
			rick_mc.gotoAndStop("walk");
		}
		else if (Key.isDown (Key.LEFT) )
		{
			rick_mc._xscale = -100;
			rick_mc.stance=0;
			rick_mc._x-=rick_mc.step;
			rick_mc.gotoAndStop("walk");
		}
		else if (Key.isDown (Key.DOWN) )
		{
			rick_mc.gotoAndStop("crouch");
			rick_mc.crouch=true;
		}
		else if (!Key.isDown ())
		{
			rick_mc.gotoAndStop("stance");
			rick_mc.crouch=false;
		}
	}
	
	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;
}

Nifty eh?

In part 4 we will explore making him jump correctly (a little trickier), and executing his special sliding move.

 

 
 
Name: Kris Foxton
Location: Fukuoka, Japan
Age: 28
Flash experience: 1 year. A keen-to-learn newb.
Job: Famous nobody
Website: http://ydjapan.blogspot.com/
 
 
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048