Search code examples
xna2d

How to make a jump sprite


I'm building a Mega Man clone for a school project and I just finished the code to make my character jump and it works perfectly. But I need it to display the actual jump sprite of Mega Man. This is the whole code in Game1: http://pastebin.com/py7Y7mtD The AnimatedSprites jt helps you decide the direcrtion and display the sprite. Doesn't matter right now if it's efficient. It's just for starts. When I'll get to more complex stuff I'll work on it. MegamanJump has the jump sprite(5 or 6 sprites displayed from left to right) MegamanJump_Flip is the same thing but to the left(again, forget about efficiency).


Solution

  • Basically you want to switch out the current drawable sprite with the sprite you want to draw depending on moving direction or moving direction + jump.

    Since you already have a running update method, all you have to do is write another method that does this for you, a state variable to track what is happening and the actual states it can have, preferrably in an enum for clarity.

    Essentially

    private void swapSprite() {
       if(state == CharacterState.LEFT) {
         sprite = leftDrawable;
       }
       //etc other single ifs
       if(state == CharacterState.LEFTJUMPING) {
         sprite = leftJumpDrawable;
       }
    }
    

    Change the states on button press combinations, call this method after checking for movement.

    I hope this helped!