如何在Java游戏库(Slick2D)中同时使用运动和动画
有人请帮帮我,我卡住了。我想按向上、右、左、下,然后朝那个方向移动我的动画,使它看起来像我的播放器正在行走。但这是我的问题,我提出了一个解决方案,但是Java会打开程序,然后很快关闭它。我知道这意味着有一个错误,但我不能阅读编译器错误消息,因为它没有说什么,它只是告诉我我的错误所在的行号。有人请帮帮我,我对此很失望如何在Java游戏库(Slick2D)中同时使用运动和动画,java,slick2d,Java,Slick2d,有人请帮帮我,我卡住了。我想按向上、右、左、下,然后朝那个方向移动我的动画,使它看起来像我的播放器正在行走。但这是我的问题,我提出了一个解决方案,但是Java会打开程序,然后很快关闭它。我知道这意味着有一个错误,但我不能阅读编译器错误消息,因为它没有说什么,它只是告诉我我的错误所在的行号。有人请帮帮我,我对此很失望 package test; import org.newdawn.slick.*; import org.newdawn.slick.state.*; public class
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;
}
}
您的问题是试图将全局变量currentImage初始化为尚未初始化的动画。使用slick,您需要在init方法中处理初始化问题。使用java编程时,在尝试使用变量初始化另一个变量之前,请始终初始化变量 要解决您的问题,请更改:
private Animation currentImage = MoveRightAni;
致:
在您的init方法中,在map=之后的最后一行
currentImage = MoveRightAni;
这可能有助于发布错误所指的行。发布堆栈跟踪提供给您的所有内容。从我所看到的来看,您的代码看起来足够好,可以按预期运行。org.newdawn.slick.SlickException:Game.render failure-检查游戏代码。java.lang.nullpointerException非常感谢,我不知道我怎么会错过它:我真的很感谢,谢谢,没问题。别忘了接受答案:
currentImage = MoveRightAni;