Search code examples
javaanimationframespriteslick2d

Slick Animation - There must be one duration per frame


I am trying to make a game in Slick, and before I start I want to test the helicopter animation I will be using. It just opens and then immediately closes with these errors:

Exception in thread "main" java.lang.RuntimeException: There must be one duration per frame at org.newdawn.slick.Animation.(Animation.java:111) at javagame.Menu.init(Menu.java:22) at 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) at 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 the help! If I am completely wrong on how I am going about this, I am sorry. I could't find a decent tutorial to save my life!


Solution

  • the problem is that you are passing the Animation constructor 4 Images and only 2 duration values, 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;
        }