Robot Foot Games Helping you improve your games as we improve our own. :)

30May/112

Adding melee to PSK

Update: Went through the entire tutorial in a fresh solution and fixed one bug where the enemies no longer killed you. The tutorial has been updated (UpdateEnemies(...) method in Level.cs) and now works as-is. If you have problems, make sure to download the file just below and compare it with yours to see where you may have messed up!

You can download a working version below.

 

Here's a video showing off the final product of this tutorial:

Here is what I used for his attack animation as a placeholder, feel free to use it. Place this in the same place as the other animations: “HighResolutionContent>Sprites>Player”. Also make sure to add it to your project: Right click on the Player folder and Add>Existing Item>Attack.png

 

 


 

Player.cs

Find the following:

After it add the following:

 

Next find the following:

After that add the following:

 

At the top of Player.cs find the following line:

After that add the following line:

 

Now find the LoadContent method in Player.cs and find the following line:

After that add the following:

 

Find the DoJump method and add a new method after it:

 

Now our attack is setup but we need a keybind so we can actually use it! At the very top of our player class add just the KeyboardState line below:

Now add the following in the GetInput method:

 

Now onto the Update method, after the GetInput() call add the following:

 

Find the following in the Update method:

And change it to look like the following:

 

BUILD THE SOLUTION AT THIS POINT TO MAKE SURE YOU DON’T HAVE ANY ERRORS.

 

Enemy.cs

In Enemy.cs find the following:

and add the following after it:

 

Here we added a death animation, death sound, and an IsAlive variable that will allow our enemies to die. We also added a timer so we can make our enemy disappear a certain amount of time after he dies. If you want to change how long they're alive simply change the deathTimerMax variable.

To initialize the IsAlive variable find the constructor for our Enemy, and after,

add:

 

First we need to load the sound into the project because it isn’t there by default. In the solution explorer go to “SharedContent” right click on Sounds>Add>Existing Item and add “MonsterKilled”. Right click on MonsterKilled and go to Properties. In the Properties window find ContentProcessor and from the drop down menu to the right of that choose Sound Effect – XNA Framework. At the very top add another using statement:

Once you add the sound, you'll need to change it's properties because it's considered a 'Song' instead of a 'Sound Effect'. Right click on the sound after you add it to your solution and hit properties. Under 'Content Processor' change it from Song to Sound Effect. We need to load the new sound so find the LoadContent method and add the following at the bottom:

While we’re in the LoadContent method add the following by the other animations: (NOTE: You also need to add this animation to your solution because it isn't there by default. Navigate to HighResContent>Sprites and then in each folder (MonsterA, MonsterB, etc) you'll have to add the existing item like you did above with the sound)

 

Inside the Update method add this block of code.

 

Inside of the Draw method let's change the code a little bit. Change the if/else statement to look like the following:

 

Under the Draw method add a new method:


 

Level.cs

Next in Level.cs find the following in the UpdateEnemies method (and add the bold part)

Now add this if statement after the one above:

 

Now under the UpdateEnemies method add a new method:

 

Now in the Draw(...) method find the block of code to draw the enemies and change it to this:

 

And that's it! Give it a try and see if you can finally kill those bad guys!

Comments (2) Trackbacks (0)
  1. Hi, I wonder how would you do that in Windows Phone with touch countrols?

    • It depends on the game. You could do touch screen controls so you could jump/melee/move with the on-screen controls. You could also move with the accelerometer and tap on enemies to attack them. The XNA website has examples for both so you can toy around with those to see which idea you like better.


Leave a comment

(required)

No trackbacks yet.