Java,光滑2d,动画没有动画

时间:2013-09-29 17:05:44

标签: java animation slick2d

环顾四周,没有看到任何与我的特定问题相符的问题,我正试图从精灵表中获取子图像来制作动画,使用Slick的动画方法,我得到了子图像以显示并且没有错误,但它是一个静态图像,而不是动画。这是我的代码:

变量

public class World extends BasicGameState{
Animation hero, heroUp, heroDown, heroLeft, heroRight;

Image world;
boolean showMenu = false;
int[] duration = {500,500,500};

init方法

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
Image sprite = new Image("res/Sprites.png");
world = new Image("res/world.png");

Image[] moveUp = {sprite.getSubImage(378, 106, 28, 33),sprite.getSubImage(411, 105, 28, 33),sprite.getSubImage(346, 105, 28, 33)};

Image[] moveDown = {sprite.getSubImage(378, 10, 28, 33),sprite.getSubImage(411, 9, 28, 33),sprite.getSubImage(346, 10, 28, 33)};

Image[] moveLeft = {sprite.getSubImage(378, 42, 28, 33),sprite.getSubImage(411, 41, 28, 33),sprite.getSubImage(346, 42, 28, 33)};

Image[] moveRight = {sprite.getSubImage(378, 74, 28, 33),sprite.getSubImage(411, 73, 28, 33),sprite.getSubImage(346, 74, 28, 33)};

heroUp = new Animation(moveUp, duration, false);
heroDown = new Animation(moveDown, duration, false);
heroLeft = new Animation(moveLeft, duration, false);
heroRight = new Animation(moveRight, duration, false);

hero = heroDown;
    }

和我的更新方法

public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException {
    Input input = gc.getInput();
    if(input.isKeyDown(Input.KEY_W)){
        hero = heroUp;
        heroY+=delta*.1f;
        if(heroY>300){
            heroY-=delta*.1f;
        }
    }
    if(input.isKeyDown(Input.KEY_S)){
        hero = heroDown;
        heroY-=delta*.1f;
        if(heroY<-3698){
            heroY+=delta*.1f;
        }
    }
    if(input.isKeyDown(Input.KEY_A)){
        hero = heroLeft;
        heroX+=delta*.1f;
        if(heroX>406){
            heroX-=delta*.1f;
        }
    }
    if(input.isKeyDown(Input.KEY_D)){
        hero = heroRight;
        heroX-=delta*.1f;
        if(heroX<-3670){
            heroX+=delta*.1f;
        }
    }
    if(input.isKeyDown(Input.KEY_ESCAPE)){
        showMenu=true;
    }
    if(showMenu==true){
        if(input.isKeyDown(Input.KEY_R))
            showMenu=false;
        if(input.isKeyDown(Input.KEY_M)){
            showMenu=false;
            sbg.enterState(0);
        }
        if(input.isKeyDown(Input.KEY_Q))
            System.exit(0);
    }
}

1 个答案:

答案 0 :(得分:0)

在移动逻辑之后尝试在更新方法中放置hero.update(delta);。它告诉动画它的运行速度。