Hello,
I've got a problem with an animation. I want it to stop when no key is pressed and to run as long as some key is held down.
But when stopped, it won't start again, and of course, I don't know why.
I used the platformer tutorial to set up my environment (
http://slick.cokeandcode.com/wiki/doku. ... _tutorials)
Animation declaration:
Code:
public class SlickTest extends BasicGame {
...
private Animation currentAnimation;
private Animation right;
private boolean keyPressed = false;
public void init(GameContainer container) throws SlickException {
container.setVSync(true);
Image pokemonSpriteSheet = new Image("data/pokemon125.png");
SpriteSheet playerSpriteSheet = new SpriteSheet(pokemonSpriteSheet,24,33);
map = new BlockMap("data/lvl-01.tmx");
right = new Animation();
right.addFrame(playerSpriteSheet.getSubImage(145, 1, 24, 33).getFlippedCopy(true, false), 150);
right.addFrame(playerSpriteSheet.getSubImage(170, 1, 24, 33).getFlippedCopy(true, false), 150);
right.addFrame(playerSpriteSheet.getSubImage(196, 1, 24, 33).getFlippedCopy(true, false), 150);
right.setLooping(true);
right.setAutoUpdate(true);
currentAnimation = right;
}
Update method:
Code:
public void update(GameContainer container, int delta) throws SlickException {
Input input = container.getInput();
boolean keyPressed = false;
if (input.isKeyDown(Input.KEY_RIGHT)) {
currentAnimation = right;
keyPressed = true;
}
if (keyPressed)
{
if (currentAnimation.isStopped()) {
currentAnimation.restart();
}
}
else
{
currentAnimation.stopAt(0);
}
}
Finally, the render and main methods:
Code:
public void render(GameContainer container, Graphics g) {
BlockMap.tmap.render(0, 0);
g.drawAnimation(currentAnimation, playerX, playerY);
}
public static void main(String[] argv) throws SlickException {
AppGameContainer container = new AppGameContainer(new SlickTest(), 640, 480, false);
container.start();
}
I saw in the API that I should use start or restart but it doesn't work.
For information, the animation works fine when initialized and is never stopped.
In fact, I just can't start or restart it after having stopped it.
Thanks in advance if you have a clue to share.
Gaff.