How do you set the movement speed of a sprite?

Posted by rphello101 on Game Development See other posts from Game Development or by rphello101
Published on 2012-10-07T17:37:58Z Indexed on 2012/10/07 21:55 UTC
Read the original article Hit count: 133

Filed under:
|
|
|

I'm using Slick 2D/Java to play around with graphics. Getting an image to move is easy:

Input input = gc.getInput();

        if(input.isKeyDown(sprite.up)){
            sprite.y--;
        }else if (input.isKeyDown(sprite.down)){
            sprite.y++;
        }else if (input.isKeyDown(sprite.left)){
            sprite.x--; 
        }else if (input.isKeyDown(sprite.right)){
            sprite.x++; 
        }

However, this is called on every update, so if you hold up, the sprite moves to the edge of the screen in a few hundred milliseconds. Since coordinates are integers, I can't add less than 1 to slow the sprite down. I'm assuming I must have to implement a timer of some sort or something. Any advice?

© Game Development or respective owner

Related posts about java

Related posts about sprites