光滑动画 - 每帧必须有一个持续时间

时间:2012-12-19 23:47:11

标签: java animation frame sprite slick2d

我正在尝试在Slick中制作游戏,在开始之前我想测试我将要使用的直升机动画。它只是打开然后立即关闭这些错误:

线程“main”中的异常java.lang.RuntimeException:每帧必须有一个持续时间     在org.newdawn.slick.Animation。(Animation.java:111)     在javagame.Menu.init(Menu.java:22)     在javagame.Game.initStatesList(Game.java:19)     在org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:170)     在org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:433)     在org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:357)     在javagame.Game.main(Game.java:29)

这是我的代码:

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;
        }
    } 

感谢您的帮助!如果我对这个问题完全错误,我很抱歉。我找不到一个体面的教程来拯救我的生命!

1 个答案:

答案 0 :(得分:3)

问题是你传递的是动画构造函数4个图像,只有2个持续时间值,试试这个:

                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;
    }