Stay Updated!



Designed by:

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

Player.cs

lt;span style="font-size: 10pt;">Find the following:

private float jumpTime;


After it add the following:

// Attacking state
public bool isAttacking;
const float MaxAttackTime = 0.33f;
public float AttackTime;


Next find the following:

public Rectangle BoundingRectangle
{    
    get    
    {        
        int left = (int)Math.Round(Position.X - sprite.Origin.X) + localBounds.X;
        int top = (int)Math.Round(Position.Y - sprite.Origin.Y) + localBounds.Y;

        return new Rectangle(left, top, localBounds.Width, localBounds.Height);
    }
}


After that add the following:

public Rectangle MeleeRectangle
{
    get
    {
        int left = (int)Math.Round(Position.X - sprite.Origin.X) + localBounds.X;          
        int top = (int)Math.Round(Position.Y - sprite.Origin.Y) + localBounds.Y;
        if (flip == SpriteEffects.FlipHorizontally)
            return new Rectangle(
                (left + localBounds.Width), 
                top, 
                localBounds.Width, 
                localBounds.Height);
        else
            return new Rectangle(
                (left - localBounds.Width), 
                top, 
                localBounds.Width, 
                localBounds.Height);
    }
}


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

private Animation dieAnimation;


After that add the following line:

private Animation attackAnimation;


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

dieAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/Die"), 0.1f, false);


After that add the following:

attackAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/Attack"), 0.1f, false);


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

private void DoAttack(GameTime gameTime)
{
    // If the player wants to attack
    if (isAttacking)
    {
        // Begin or continue an attack
        if (AttackTime > 0.0f)
        {
            AttackTime -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            sprite.PlayAnimation(attackAnimation);
        }
        else
        {
            isAttacking = false;
        }
    }
    else
    {
        //Continues not attack or cancels an attack in progress
        AttackTime = 0.0f;
    }
}


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:

class Player
{
    KeyboardState previousKeyboardState = Keyboard.GetState();


Now add the following in the GetInput method:

if (previousKeyboardState.IsKeyUp(Keys.F) && keyboardState.IsKeyDown(Keys.F))
{
    if (AttackTime != MaxAttackTime)
    {
        isAttacking = true;
        AttackTime = MaxAttackTime;
    }
}


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

DoAttack(gameTime);


Find the following in the Update method:

if (IsAlive && IsOnGround)
{
    if (Math.Abs(Velocity.X) - 0.02f > 0)
    {
        sprite.PlayAnimation(runAnimation);
    }
    else
    {
        sprite.PlayAnimation(idleAnimation);
    }
}


And change it to look like the following:

if (IsAlive && IsOnGround)
{
    if (isAttacking)
    {
        sprite.PlayAnimation(attackAnimation);
    }
    else
    {
        if (Math.Abs(Velocity.X) - 0.02f > 0)
        {
            sprite.PlayAnimation(runAnimation);
        }
        else
        {
            sprite.PlayAnimation(idleAnimation);
        }
    }
}


Build the solution at this point to make sure you don’t have any errors.




 

Add comment


Security code
Refresh