Stay Updated!



Designed by:

XNA Tutorials
XNA Platformer Starter Kit - Adding Melee - Enemy.cs
Written by Jeff Brown   
Article Index
XNA Platformer Starter Kit - Adding Melee
Player.cs
Enemy.cs
Level.cs
All Pages

Enemy.cs


In Enemy.cs find the following:

// Animations
private Animation runAnimation;
private Animation idleAnimation;
private AnimationPlayer sprite;


and add the following after it:

private Animation dieAnimation;

// Sounds
private SoundEffect killedSound;

public bool IsAlive { get; private set; }


Here we added a death animation, death sound, and an IsAlive variable that will allow our enemies to die. To initialize the IsAlive variable find the constructor for our Enemy, and after

this.position = position;

add:

this.IsAlive = true;


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:

using Microsoft.Xna.Framework.Audio;


We need to load the new sound so find the LoadContent method and add the following at the bottom:

//Load sounds. 
killedSound = Level.Content.Load<SoundEffect>("Sounds/MonsterKilled");  


While we’re in the LoadContent method add the following by the other animations:

dieAnimation = new Animation(Level.Content.Load<Texture2D>(spriteSet + "Die"), 0.07f, false);


Under the Update method add a new method:

public void OnKilled(Player killedBy)
{
    IsAlive = false;
    killedSound.Play();
}


 

Add comment


Security code
Refresh