如何在Java游戏库(Slick2D)中一起使用移动和动画

时间:2014-05-07 06:35:06

标签: java slick2d

有人请帮助我,我被卡住了。我想按向上,向右,向左,向下移动我的动画向这个方向移动,看起来我的玩家正在走路。但这是我的问题,我提出了一个解决方案,但Java将打开程序,但然后很快关闭它。我知道这意味着有一个错误,但我无法读取编译器错误消息,因为它没有说什么,它只是告诉我我的错误所在的行号。有人请帮助我,我对此感到非常沮丧。

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



public class Play extends BasicGameState
{
    private SpriteSheet MoveRight; // initate a SprtieSheet
    private Animation MoveRightAni; // initate a Animation

    private SpriteSheet MoveLeft; // initate a SprtieSheet
    private Animation MoveLeftAni; // initate a Animation

    private SpriteSheet MoveUp; // initate a SprtieSheet
    private Animation MoveUpAni; // initate a Animation

    private SpriteSheet MoveDown; // initate a SprtieSheet
    private Animation MoveDownAni; // initate a Animation


    private Animation currentImage = MoveRightAni;

    private Image map; 
    public float x = 0;
    public float y = 0;




    public Play( int state){

    }


    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
        MoveRight = new SpriteSheet("test/MoveRight.png",21,24); // declare a SpriteSheet and load it into java with its dimentions
        MoveRightAni = new Animation(MoveRight, 400); // declare a Animation, loading the SpriteSheet and inputing the Animation Speed

        MoveLeft = new SpriteSheet("test/MoveLeft.png",21,24); // declare a SpriteSheet and load it into java with its dimentions
        MoveLeftAni = new Animation(MoveLeft, 400); // declare a Animation, loading the SpriteSheet and inputing the Animation Speed

        MoveUp = new SpriteSheet("test/MoveUp.png",21,24); // declare a SpriteSheet and load it into java with its dimentions
        MoveUpAni = new Animation(MoveUp, 400); // declare a Animation, loading the SpriteSheet and inputing the Animation Speed

        MoveDown = new SpriteSheet("test/MoveDown.png",21,24); // declare a SpriteSheet and load it into java with its dimentions
        MoveDownAni = new Animation(MoveDown, 400); // declare a Animation, loading the SpriteSheet and inputing the Animation Speed

        map = new Image("test/map.png");



    }


    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
        g.drawImage(map, 0, 0);
        currentImage.draw(x,y);



    }

    public void update (GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
    {
        MoveRightAni.update(delta); // this line makes sure the speed of the Animation is true
        MoveUpAni.update(delta); // this line makes sure the speed of the Animation is true
        MoveLeftAni.update(delta); // this line makes sure the speed of the Animation is true
        MoveDownAni.update(delta); // this line makes sure the speed of the Animation is true
        Input input = gc.getInput();

        if (input.isKeyDown(Input.KEY_W))
        {
            y -= 0.1 * delta;
            currentImage = MoveUpAni;

        }
        if (input.isKeyDown(Input.KEY_A))
        {
            x -= 0.1 * delta;
            currentImage = MoveLeftAni;
        }
        if (input.isKeyDown(Input.KEY_D))
        {
            x += 0.1 * delta;
            currentImage = MoveRightAni;
        }
        if (input.isKeyDown(Input.KEY_S))
        {
            y += 0.1 * delta;
            currentImage = MoveDownAni;
        }


    }
    public int getID(){
        return 0;
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题是您正在尝试将全局变量currentImage初始化为尚未初始化的动画。有了光滑,你想做任何处理init()方法初始化的事情。在java中编程时,总是在尝试使用它们初始化另一个变量之前初始化变量。

要解决您的问题,请更改:

private Animation currentImage = MoveRightAni;

为:

private Animation currentImage;

并在map = ...

之后的最后一行的init()方法中
currentImage = MoveRightAni;