Making a Beat-Em-Up Fighting Game in Flash: Part SIX

OK time for the fun stuff, WEAPONS!!

Making your character able to pick up weapons won't be much of a stretch to code if you've been following me up until now. Don't worry about having to rip the sprites from a gif- I'll include them all in zip from now on.

Grab them HERE

Let's analyse what happens (in code) when a character picks up a weapon. First we put the weapon somewhere on the screen. Say, at x:250 y;300, -anywhere'll do. Then we want to say "Okay, so if Rick is touching the mc (hitTest) and we have DOWN pressed, pick up the weapon (attach weapon to rick_mc instead of _root).
Then we have to set a variable to tell us that rick is carrying a weapon, in order to alter the orientation of the weapon with rick's movements at a later stage. We also have to edit our enemy code now, to let them know that they've had their head chopped off instead of being punched or kicked (there's a difference). If the enemy has 'health' we will need to alter how much it decreases when hit by a cleaver instead of a fist.

But let's piss around with all of that later. Firstly, let's just get the art side working. Study the following picture;

pic61.jpg

As you can see, the weapon has frames of own. We have to build our own movieclip. In most games, weapons are 'layers' that are put on top of the character that's holding them. Judging by the Mame rom snaps for the meat-cleaver weapon;-

pic63.gif

SplatterHouse weapons are manipulated in the same way.

Using a weapon also adds a couple of new animations for the rick_mc, one of him swinging the blade, and a single frame for picking it up. You can go ahead and add those right away to the end of your current mc. Make sure to name the frames that contain the new movieclips 'chop' and 'pickup'.

pic64.gif

Now comes the tricky part. Getting weapons to fit on the character exactly is sometimes a trial by error process. Firstly, create a new movieclip (Insert New Symbol/MovieClip) and call it 'cleaver'. Essentially what we do now is create an mc similar in nature to the rick_mc, but for weapons. And the really great part is you get to do this for every single weapon you create in the game!

What you do is in the first frame you keep the stationary floor-based movieclip for Rick to pick up. You then stick cleaver1.png in one frame and cleaver1invert.png in another and align them up. In SplatterHouse the weapons only flash for about a millisecond, so in order to copy that effect you need to extend the first frame (by using F5) until it's about 20 frames long. So make a new movieclip symbol to do all that in ('cleaverflick'), and then go into 'cleaver' and add this new symbol to the first frame.

pic65.gif

Good. Next let's deal with the 'pickup' frame. For this and the rest of the cleaver animations, we have to be careful to orientate them correctly with Rick. So instead of just plopping cleaver3.png anywhere on the screen, we're going to make sure it fits Ricks hand by building it in the rick_mc first.
So, to do this skip out of the 'cleaver' symbol and open up 'rick_mc'. What we need to do is create a temporary layer for our weapon, position it, THEN create the new mc symbol. So go to Insert/Timeline/Layer (or right-click any layer and press Insert Layer). It should appear in the center. Next lock all the layers except for this new one (click on the right hand bullet next to the layer names) and on the new layer, on the 'pickup' frame tap F6 to create a new keyframe at that spot. Now, drag cleaver3.png from the library and position it to Ricks hand accordingly. Next click on the image, tap F8 New Movieclip, call it 'cleaverpickup'. Now the important part, select the new movieclip and right-click Copy. Get out of rick_mc and back into cleaver, then whilst making sure you are in the new 'pickup' frame, go to Edit/Paste in Place. Voila, it should line up exactly to Rick now (when we attach the entire mc later on). Don't you dare reposition/align anything from here on in. You can now delete the image back in rick_mc (but not the new layer yet).

pic66.gif

Next we have to start making animated movieclips for the weapons. Get's trickier from here on in, so bear with me.

Let's deal with the chop animation. Go into rick_mc, then into the rickchop level. What you need to do next is create another temporary layer, so Edit/Insert Layer as before. Now select the entire layer so that every frame is blacked-in and right-click Convert to Keyframes. Lock the layer that contains Rick swinging with nothing and in the first frame of the temp layer drag cleaver7.png to the proper place. 2nd frame, cleaver3.png, 3rd frame cleaver5.png and ecetera. Right NOW, we have to make a symbol for the new animations, so select the entire weapon layer again (so all the frames are blacked out) then right-click CUT. Go Insert New Symbol/New Movieclip/'cleaverchop', and within this new mc go to the first frame, right-click and paste. We're not quite done yet though.
Go back into rick_mc and above the frame of him chopping, in the temp layer, create a keyframe (F6) and now drag the 'cleaverchop' mc from the library onto the Stage and position it so that it matches Ricks hands correctly.

pic67.gif

Again, copy this image, then go into the cleaver movieclip and in your 'chop' frame Paste in Place.

So there you have it, repeat the process for every animation, naming each weapon frame the same name as the rick_mc frame to complicate things a little less.
When it's all done, you can go into rick_mc and delete all of your temp layers, including the one the first level. Don't forget to add stop() to the frames in the cleaver mc. There's an fla at the end of this tutorial too so don't scream like a girl if you didn't get the explanation.

Next up, let's get Rick actually moving correctly with the weapon, and following that we'll adjust the zombie code correctly to deal with the new death sequence. To start. Let's get that weapon on the screen. Go into your code, and at the very top type;

_root.attachMovie("cleaver", "cleaver", 9 , {_x:250, _y:300});
Which attaches our weapon dynamically. We now have an mc we can refer to by the name 'cleaver' at depth 9. Run the file and you should see it appear, flashing away. This is done by using attachmovie. As you might or might not know, all Flash is, is movies. Even the stage is an mc in itself, titled level0. Put this code at the very top of your AS script.

function CheckForWeapon()
{
	if(rick_mc.hitTest(cleaver) and rick_mc.crouch)
	{
		removeMovieClip(cleaver);
				
		rick_mc.attachMovie("cleaver", "weapon", 9);
		rick_mc.weapon.gotoAndStop("pickup");
		rick_mc.pickup=true;
		rick_mc.weaponheld=true;
	}
	
	
		
		
		
};

So what this does is it checks for Ricks proximity to the weapon and if he is crouching (rick_mc.crouch on its own is the same as rick_mc.crouch==true). If so it deletes the cleaver movieclip from the screen and creates a new instance of it, which is then set to the 'pickup' frame. We've also jus t created two new variables to rick_mc pickup and weaponheld, and changed the mc instance name from 'cleaver' to 'weapon'. So accordingly we should add them to the Initalise function for Rick.

function Initialize()
{
     rick_mc.step=15;
     rick_mc.attack = false;
     rick_mc.stance= 1;  
     rick_mc.crouch=false;
     keydown=false;

     rick_mc.uvel=40;
     rick_mc.gravity=6;
     rick_mc.jump=false;

     //New variables:

     rick_mc.pickup=false;
     rick_mc.weaponheld=false;
}
 
And put the following line within the main onEnterFrame event to check this function at the speed of the framerate.
if(!rick_mc.weaponheld)
		CheckForWeapon();

The next important bit is editing the section that deals with Rick crouching so that it can also be used for picking-up weapons.

if (Key.isDown (Key.DOWN) )
		{
			 if(rick_mc.weaponheld && !rick_mc.pickup)
					rick_mc.weapon.gotoAndStop("crouch");
			
			if(rick_mc.pickup)
			{
				rick_mc.gotoAndStop("pickup");
				rick_mc.weapon.gotoAndStop("pickup");
				rick_mc.pickup=false;
			}				



The rest is pretty straightforward, we simply need to add another check to every movement so that not only is the rick_mc frame changed but also the weapon frame.

function Jump()
{
	
	if (Key.isDown (Key.LEFT) )
		{
			
			rick_mc.stance=0;
			rick_mc._x-=rick_mc.step;
			
			
			

		}
	else if (Key.isDown (Key.RIGHT) )
		{
			
			rick_mc.stance=1;
			rick_mc._x+=rick_mc.step;
			
			
		}
		
	 
		
		if(!rick_mc.attack)
		{
		
		
		if (Key.isDown (Key.DOWN))
		{
			 if(rick_mc.weaponheld)
					rick_mc.weapon.gotoAndStop("crouch");
					
			rick_mc.gotoAndStop("crouch");
			rick_mc.crouch=true;
		}
		else if (!Key.isDown () )
		{
			if(rick_mc.weaponheld)
					rick_mc.weapon.gotoAndStop("jump");
			
			rick_mc.gotoAndStop("jump");
			rick_mc.crouch=false;
			
		}
		
		
		
		}
		
	
	if (Key.isDown (Key.CONTROL) && keydown==false)
		{
			rick_mc.attack=true;			
		}	
		
			
		
		rick_mc._y-=rick_mc.uvel;
		rick_mc.uvel-=rick_mc.gravity;
		
		
		if(rick_mc._y+20 > ground_mc._y-115)
		{
			rick_mc.jump=false;
			rick_mc._y=ground_mc._y-115;
			rick_mc.uvel=40;
			
		}
}


function Movement()
{
	
		if (Key.isDown (Key.RIGHT) )
		{
			
			rick_mc.stance=1;
			rick_mc._x+=rick_mc.step;
			rick_mc.gotoAndStop("walk");
			if(rick_mc.weaponheld)
				rick_mc.weapon.gotoAndStop("walk");
			
		}
		else if (Key.isDown (Key.LEFT) )
		{
			
			rick_mc.stance=0;
			rick_mc._x-=rick_mc.step;
			rick_mc.gotoAndStop("walk");
			if(rick_mc.weaponheld)
				rick_mc.weapon.gotoAndStop("walk");

		}
		else if (Key.isDown (Key.DOWN) )
		{
			 if(rick_mc.weaponheld && !rick_mc.pickup)
					rick_mc.weapon.gotoAndStop("crouch");
			
			if(rick_mc.pickup)
			{
				rick_mc.gotoAndStop("pickup");
				rick_mc.weapon.gotoAndStop("pickup");
				rick_mc.pickup=false;
			}				
			else
			{
			   rick_mc.gotoAndStop("crouch");
			}
			  
			   
			rick_mc.crouch=true;
		}		
		else if (!Key.isDown () )
		{
			rick_mc.gotoAndStop("stance");
			rick_mc.crouch=false;
			if(rick_mc.weaponheld)
				rick_mc.weapon.gotoAndStop("stance");
			
		}
		
		//Jump check needs to be out of the else..if loop or character
		//will not jump while walking.
		
		if (Key.isDown (Key.UP))
		{
			rick_mc.gotoAndStop("jump");
			rick_mc.jump=true;
		}		
		
			
		if (Key.isDown (Key.CONTROL) && keydown==false)
		{
			rick_mc.attack=true;			
		}	
	
};


function Attack()
{
	
	if(rick_mc.jump)
	{
		rick_mc.gotoAndStop("crouchkick");
		 if(rick_mc.weaponheld)
					rick_mc.weapon.gotoAndStop("crouchkick");
	}
	else
	{
		if (Key.isDown (Key.DOWN))
		{
			 if(rick_mc.weaponheld && !rick_mc.pickup)
					rick_mc.weapon.gotoAndStop("crouchkick");
					
			rick_mc.gotoAndStop("crouchkick");
		}
			else
		{
			if(rick_mc.weaponheld)
			{
				rick_mc.weapon.gotoAndStop("chop");
				rick_mc.gotoAndStop("chop");
			}
			else
			{
				rick_mc.gotoAndStop("punch");
			}
		}                                  
	     
	}	
	
	
	
};

Chapter 7 will be all about health and damage. We'll give both Rick and the zombies the ability to lose health and die, aswell as cover dropping weapons when you get hit.

DOWNLOAD THE FLA

Questions/advice/tutorial errors: email to youngdude77@hotmail.com

GO TO PART 7