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;
}
}
答案 0 :(得分:0)
您的问题是您正在尝试将全局变量currentImage初始化为尚未初始化的动画。有了光滑,你想做任何处理init()方法初始化的事情。在java中编程时,总是在尝试使用它们初始化另一个变量之前初始化变量。
要解决您的问题,请更改:
private Animation currentImage = MoveRightAni;
为:
private Animation currentImage;
并在map = ...
之后的最后一行的init()方法中currentImage = MoveRightAni;