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've been working on a top-down, and the player already has animations for moving left and right. However, if I move a certain distance and stop, it will stop on the walking animation, which isn't logical as opposed to a standing animation while the player is idle.

I've been using this input handler class:

public class InputHandler implements KeyListener
{       
public boolean up = false;
public boolean down = false;
public boolean left = false;
public boolean right = false;

public InputHandler(Game game)
{
    game.addKeyListener(this);
}

public void toggle(KeyEvent ke, boolean Pressed)
{
    int KeyCode = ke.getKeyCode();

    if(KeyCode == ke.VK_UP) up = Pressed;
    if(KeyCode == ke.VK_DOWN) down = Pressed;
    if(KeyCode == ke.VK_LEFT) left = Pressed;
    if(KeyCode == ke.VK_RIGHT) right = Pressed;
}

public void keyPressed(KeyEvent e)
{
    toggle(e, true);
}

public void keyReleased(KeyEvent e)
{
    toggle(e, false);
}

public void keyTyped(KeyEvent e)
{

}
}

and it is coupled with the tick() method from my main class:

private void tick()
{
    if(input.right)
    {
        xScroll++;

        if(xScroll / 16 % 2 == 0)
        {
            dirFacing = 2;
        }
        if(xScroll / 16 % 2 != 0)
        {
            dirFacing = 4;
        }
    }

    if(input.left)
    {
        xScroll--;

        if(xScroll / 16 % 2 == 0)
        {
            dirFacing = 1;
        }
        if(xScroll / 16 % 2 != 0)
        {
            dirFacing = 3;
        }
    }

    if(input.down)
    {
        yScroll++;
    }

    if(input.up)
    {
        yScroll--;
    }
}

Again, the above yScroll will be added later, I really just need to solve the standing animation first.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

One way to solve this is to add a check to see if none of direction flags are set. Then use that case to start the idle animation in the tick().

For instance:

if(!input.up && !input.down && !input.right && !input.left) { //play idle animation this frame }

Another thing to consider is the direction of the idle animation. If your character is walking left, and the idle animation shows them looking down, when the player stops moving the character will suddenly change directions. This can be solved with having 4 different idle animations. Then you use the dirFacing from your last tick to determine which one to play.

One last point is you aren't checking the time between ticks anywhere. If you store the delta time between ticks you can make the animation play at the same rate no matter how fast or slow the tick() method gets called

share|improve this answer
    
Yeah I've really tried to skimp out on timed things since i don't want to have to deal with threads (I'm still kind of a beginner) although I probably will have to add them at some point even though it's just a Pokemon mock up. Thanks for the help though the !input.____ worked! –  A13X Jul 24 '12 at 19:42
1  
@A.K. I wonder how you came to the conclusion to use threads? Using delta-time has nothing to do with threads... you don't have to deal with multiple threads for that. –  bummzack Jul 25 '12 at 7:02
    
I was thinking the timing needed like the pause(500milliseconds) method or that threads were needed for measuring the time? –  A13X Jul 26 '12 at 15:20
    
The way I usually see it done is with a while loop. The while loop first checks from any input from the OS and increases a time counter every loop. Then, when the time counter goes over a minimum amount, it calls the tick() with a deltaTime in milliseconds. –  Thomas Marnell Jul 26 '12 at 15:24
    
So there is a pause before initiating movement via the tick() inputs? I can't find a great place to put the while(input != null) without it giving me a blank screen because the rest of my code didn't fire. –  A13X Jul 26 '12 at 16:16

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.