0

I'm new to programing and I have a problem. I followed TheNewBoston tutorial, added more animations and now the console says:

Exception in thread "main" java.lang.RuntimeException: There must be one duration per frame

Here's my code:

package javagame;

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

public class Play extends BasicGameState{

Animation player, movingUp, movingUp1, movingUp2, movingDown, movingDown1, movingDown2, movingLeft, movingLeft1, movingLeft2, movingRight, movingRight1, movingRight2;
Image playground;
boolean quit = false;
int[] duration = {200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200};
float playerPositionX = 0;
float playerPositionY = 0;
float shiftX = playerPositionX + 320;
float shiftY = playerPositionY + 160;


public Play(int State){
}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
    playground = new Image("res/playground.png");
    Image[] walkUp = {new Image("res/player11.png"), new Image("res/player12.png"), new Image("res/player10.png")};
    Image[] walkDown = {new Image("res/player2.png"), new Image("res/player3.png"), new Image("res/player1.png")};
    Image[] walkLeft = {new Image("res/player5.png"), new Image("res/player6.png"), new Image("res/player4.png")};
    Image[] walkRight = {new Image("res/player8.png"), new Image("res/player9.png"), new Image("res/player7.png")};



    movingUp = new Animation(walkUp, duration, false);

    movingDown = new Animation(walkDown, duration, false);

    movingLeft = new Animation(walkLeft, duration, false);

    movingRight = new Animation(walkRight, duration, false);


    player = movingDown;

}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
    playground.draw(playerPositionX, playerPositionY);
    player.draw(shiftX, shiftY);
    g.drawString("Player's X: "+playerPositionX+"\nPlayer's Y: "+playerPositionY, 400, 20);

    if(quit==true){
        g.drawString("Resume (R)", 250, 100);
        g.drawString("Main Menu (M)", 250, 150);
        g.drawString("Quit Game (Q)", 250, 200);
        if(quit==false){
            g.clear();
        }
    }
}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
    Input input = gc.getInput();

    //up
    if(input.isKeyDown(Input.KEY_UP)){
        player = movingUp;
        playerPositionY += delta * .1f;
        if(playerPositionY>162){
            playerPositionY -= delta * .1f;
        }
    }
    //down
    if(input.isKeyDown(Input.KEY_DOWN)){
        player = movingDown;
        playerPositionY -= delta * .1f;
        if(playerPositionY<-600){
            playerPositionY += delta * .1f;
        }
    }
    //left
    if(input.isKeyDown(Input.KEY_LEFT)){
        player = movingLeft;
        playerPositionX += delta * .1f;
        if(playerPositionX>324){
            playerPositionX -= delta * .1f;
    }
    //right
    if(input.isKeyDown(Input.KEY_RIGHT)){
        player = movingRight;
        playerPositionX -= delta * .1f;
        if(playerPositionX<-840){
            playerPositionX += delta * .1f;}
        }   
    }    
}    

public int getID(){
    return 1;
}

}

Thanks!

NorthCat
  • 9,643
  • 16
  • 47
  • 50
pumkin
  • 3
  • 1

1 Answers1

0

The first Google result for "there must be one duration per frame" leads to an identical question on this same site: Slick Animation - There must be one duration per frame

Your duration array needs to be the same size as your image array. Change to:

int[] duration = {200, 200, 200};

Community
  • 1
  • 1
The111
  • 5,757
  • 4
  • 39
  • 55