Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am just starting game-dev with slick2D and marte engine. When I move my sprite left and right, I am doing walk animation but when the key is released how can I decide in which side the sprite face to set?

Here's the relevant code:

    Input ip = gc.getInput();

    if (ip.isKeyDown(Input.KEY_RIGHT))
    {
        this.currentAnim = "Anim2";
        System.out.println("down");
        x += 0.1f * delta;
    }

    if (ip.isKeyDown(Input.KEY_LEFT))
    {
        this.currentAnim = "Anim4";
        System.out.println("down");
        x -= 0.1f * delta;
    }
share|improve this question
1  
Mike, I've edited your question to better fit what we expect. In the future, avoid pastebin links or zipped code (pastebin expires making the question unhelpful and no one wants to download an unknown zip file from the internet). Just post the relevant part of your code, keep the question simple an to the point. Additionally, when you get an answer that solves your problem, you accept the answer (click the check mark on the answer), don't modify the title to include "solved". Welcome to the site! –  Byte56 Oct 22 '12 at 13:39
1  
oh sorry it was my first post i'll follow the manner which you described from the next time and thank you :} –  Miko1 Oct 22 '12 at 16:57

1 Answer 1

up vote 3 down vote accepted

You might be overthinking this because this is extremely simple. Try thinking about it conceptually before you start thinking in code.

Your question is: which side should the sprite face after walking

The answer you want is: it should face the side the sprite walked towards

To decide that each frame we need to store lastDirectionWalkedTowards but lets abbreviate that a bit and call it facing. Now since your sprites move along the X-axis we only have to know if its left or right. You can create an enum for this but even easier would be to store it as an int, with +1 meaning facing right and -1 meaning facing left. We can use 0 for neutral when we havent walked yet, and use some sane default.

The complete psuedo code:

int facing = 0; //class member

//in the update method

if (ip.isKeyDown(Input.KEY_RIGHT))
{
    facing = 1;

    this.currentAnim = "Anim2";
    System.out.println("down");
    x += 0.1f * delta;
}
else if (ip.isKeyDown(Input.KEY_LEFT))
{
    facing = -1;

    this.currentAnim = "Anim4";
    System.out.println("down");
    x -= 0.1f * delta;
}
else // if no directional key was pressed
{
    if(facing == -1) // if facing left
    {
        this.currentAnim = "LookLeft";
    }
    else // if facing right or neutral
    {
        this.currentAnim = "LookRight";
    }
}
share|improve this answer
    
whoa it was that simple ?!! just a int !! Thanks a lot Roy T. you are right i should think conceptually first > Try thinking about it conceptually before you start thinking in code. Thanks your advise will help me a lot :) –  Miko1 Oct 22 '12 at 12:30
    
No problem Mike.X. btw don't forget to accept this answer (button onder the answer score) if it was the solution :) –  Roy T. Oct 23 '12 at 8:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.