Java – Smooth animation – Each frame must have a duration

Smooth animation – Each frame must have a duration… here is a solution to the problem.

Smooth animation – Each frame must have a duration

I’m trying to make a game using Slick and I want to test the helicopter animation I’ll be using before I start. It just turns on and then closes immediately with the following error:

Exception java.lang.RuntimeException in thread “main”: Each frame must have a duration
At org.newdawn.slick.Animation. (Animation.java:111)
In JavaGame. Menu.init(Menu.java:22)
In JavaGame. Game.initStatesList(Game.java:19)
At org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:170)
At org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:433)
At org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:357)
In JavaGame. Game.main(Game.java:29)

Here is my code :

package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.tests.AnimationTest;

public class Menu extends BasicGameState {

Animation sprite, fly;

public Menu(int state){

}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
            Image [] flyanimation = {new Image("res/copter1.png"), new Image("res/copter2.png"), 
                    new Image("res/copter3.png"), new Image("res/copter4.png")};
            int [] duration = {300, 300};

fly = new Animation(flyanimation, duration, false);
            sprite = fly;
        }

public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
            sprite.draw(150, 150);
        }

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
            Input input = gc.getInput();
            if(input.isKeyDown(Input.KEY_SPACE)){
                sprite = fly;
                sprite.update(delta);
            }
        }

public int getID(){
            return 0;
        }
    } 

Thanks for your help! I’m sorry if I handled it completely wrong. I can’t find a decent tutorial to save my life!

Solution

The problem is that you pass 4 images and only 2 duration values to the animation constructor, try this :

                public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
        Image [] flyanimation = {new Image("res/copter1.png"), new Image("res/copter2.png"), 
                new Image("res/copter3.png"), new Image("res/copter4.png")};
        int [] duration = {300, 300, 300, 300};

fly = new Animation(flyanimation, duration, false);
        sprite = fly;
    }

Related Problems and Solutions