Slick Forums

Discuss the Slick 2D Library
It is currently Mon May 20, 2013 12:47 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Animation won't restart
PostPosted: Thu Jun 14, 2012 8:34 am 
Offline

Joined: Thu Jun 14, 2012 8:15 am
Posts: 2
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.


Top
 Profile  
 
PostPosted: Thu Jun 14, 2012 11:58 pm 
Offline

Joined: Thu Jan 26, 2012 5:40 pm
Posts: 79
Your keyPressed boolean is never set to false other than when it is initialized, so it is always true once set to true.

That being said, your next if statement will always go to 'else', which is currentAnimation.stopAt(0);

What you need to do is have a check for if the key isn't pressed.

Code:
if (!input.isKeyDown(Input.KEY_RIGHT)) {
keyPressed = false;
}


Top
 Profile  
 
PostPosted: Fri Jun 15, 2012 11:44 am 
Offline

Joined: Thu Jun 14, 2012 8:15 am
Posts: 2
Hello Eulogy,

Thank you for your help. I didn't work though. "keyPressed" is initialized at each update() since it's a local variable.

I have cleaned my code in order to test the restart method.

Below is the updated update() method:
Code:
public void update(GameContainer container, int delta) throws SlickException
{
   Input input = container.getInput();
      
   if (input.isKeyDown(Input.KEY_LEFT)) {
      currentAnimation.stopAt(0);
   }
   if (input.isKeyDown(Input.KEY_RIGHT)) {
      currentAnimation.start();
   }
      
}


If I press LEFT the animation stop (good job Slick! It works) and if I press RIGHT, my animation should start again (boo! it doesn't work).

After the init method, this animation is running fine. Then I stop it but it won't restart at all.

Does somebody has some hint ?

Ps : Best practicce speaking, how would you stop then start an animation ?


Top
 Profile  
 
PostPosted: Fri Jun 15, 2012 4:11 pm 
Offline

Joined: Thu Jan 26, 2012 5:40 pm
Posts: 79
I haven't used start or stop. What I do is create another animation for standing still (just one frame), for each direction and switch it to that, dependant on the last movement key pressed.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Magn919 and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group