Java 使用slick2D无法移动状态
好了,有了这段代码,我有了一个可爱的小游戏。理论上,level.java应该启动Menu.java,一旦点击屏幕上的play按钮,就应该转换到Playing.java状态。不幸的是,当这种情况发生时,点击“播放”按钮,在这种情况下,至少我会在控制台中听到大量的胡言乱语。所以我要问你们的是这个。为什么会发生这种情况?有哪些可能的解决方案?有没有什么调试技巧可以用来破解我的控制台中出现的问题?级别如下:Java 使用slick2D无法移动状态,java,lwjgl,slick2d,Java,Lwjgl,Slick2d,好了,有了这段代码,我有了一个可爱的小游戏。理论上,level.java应该启动Menu.java,一旦点击屏幕上的play按钮,就应该转换到Playing.java状态。不幸的是,当这种情况发生时,点击“播放”按钮,在这种情况下,至少我会在控制台中听到大量的胡言乱语。所以我要问你们的是这个。为什么会发生这种情况?有哪些可能的解决方案?有没有什么调试技巧可以用来破解我的控制台中出现的问题?级别如下: import org.newdawn.slick.*; import org.newdawn.s
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class level extends StateBasedGame{
public static final String gamename = "Flight 1.0 ";
public static final int menu = 0;
public static final int playing = 1;
public level(String gamename){
super(gamename);
this.addState(new Menu(menu));
this.addState(new Playing(playing));
}
public void initStatesList(GameContainer gc)throws SlickException{
this.getState(menu).init(gc, this);
this.getState(playing).init(gc, this);
this.enterState(menu);
}
public static void main(String args[]){
AppGameContainer appgc;
try{
appgc = new AppGameContainer(new level(gamename));
appgc.setDisplayMode(800, 600, false);
appgc.start();
}catch(SlickException e){
e.printStackTrace();
}}}
这是带有播放按钮的菜单:
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu extends BasicGameState{
Image play;
int xpos = Mouse.getX();
int ypos = Mouse.getY();
public Menu(int state){
}
public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
play = new Image("Resources\\coin.gif");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
g.drawImage(play, 0, 0);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
Input input = gc.getInput();
if((xpos >= 0 && xpos <= 127) && (ypos>= 0 && ypos <= 33)){
if(input.isMouseButtonDown(0)){
sbg.enterState(1);
}
}
}
public int getID(){
return 0;
}
}
最后是我尝试转移到的游戏:
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Playing extends BasicGameState{
//game objects
Image guy;
Image coin1;
Image coin2;
//object coordinates
float x = 30, y = 50;
float coin1x = 1000, coin1y = 500;
float coin2x = 1100, coin2y = 400;
float rect1x = 1000, rect1y = 225;
//coin counter
int coincount = 0;
//ignore
public Playing(int state){}
//declare items
public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
guy = new Image("Resources\\PlaneMovie1.gif");
coin1 = new Image("Resources\\coin.gif");
}
//draw items
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
g.drawImage(guy, x, y);
g.drawImage(coin1, coin1x, coin1y);
g.drawImage(coin2, coin2x, coin2y);
g.drawString("Coins: " + coincount, 100, 100);
g.drawRect(rect1x, rect1y, 50, 425);
}
//declare changes to items
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
//plane movement/controls
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_SPACE)){ y -= 1 ;}
if(input.isKeyDown(Input.KEY_SPACE) == false){
y += .5;
}
//coin counter
if (x == coin2x && y == coin2y){
coincount +=1 ;
}
if(x == coin1x && y == coin1y){
coincount += 1;
}
//coin 1
coin1x --;
if(coin1x <= -100){
coin1x = 1000; coin1y -= 100;
}
if (coin1y <= 100){
coin1y = 500;
}
//coin 2
coin2x --;
if(coin2x <= -100){
coin2x = 1100; coin2y -= 100;
}
if (coin2y <= 100){
coin2y = 500;
}
//rect1
rect1x --;
if(rect1x <= -100){
rect1x = 1000; rect1y -= 100;
}
if (coin1y <= 100){
rect1y = 425;
}
}
//ignore
public int getID(){
return 1;
}
}
非常感谢您的帮助,这是我第一次发布StackOverflow,因此非常感谢建设性的批评。与图形坐标相比,Slick2d中的鼠标坐标是相反的。例如: 当您处于0,0时,鼠标Y将等于屏幕高度。我希望这能解决你的问题 编辑 进一步查看后,我发现Playing类中的coin2没有为其分配图像,因此由于nullpointer异常而崩溃,即您需要像其他类一样分配图像 以下是已编辑的更新方法,该方法更新鼠标坐标,并将Y坐标设置为与图像坐标相同的格式:
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
xpos = Mouse.getX();
// 600 is the Screen height
ypos = Math.abs(600-Mouse.getY());
if((xpos >= 0 && xpos <= 127) && (ypos>= 0 && ypos <= 33)){
if(Mouse.isButtonDown(0)){
sbg.enterState(1);
}
}
}
具体在哪种情况下?点击播放按钮以传输状态?如果是这样的话,我想这就是解决方案。我更新了我的答案,加入了更多关于更新坐标的细节。Y坐标仍然是倒转的。请注意,当我在更新的部分中添加坐标时,它所做的只是移动坐标,使其与您所说的slick2D匹配,翻转正确?嗯,在按钮应该位于的较新区域中单击时,我得到了相同的崩溃。绕过反转的Y坐标的一种方法是执行ypos=Math.abs600-Mouse.getY;//600为窗高。你能提供更多关于它如何删除按钮的详细信息吗?它没有删除按钮本身,因为我的小图形仍然在那里,但是可点击区域在右下角,而不是左上角100%翻转,由你的位解决^^^但是一旦点击可点击区域,不是Playingedit:我说的playingstate是指被列为1个正在执行的状态,整个程序崩溃