好的这段代码,我把一个可爱的小游戏扔在了一起。理论上,level.java应该启动Menu.java,一旦在屏幕上点击播放按钮,你应该转移到Playing.java状态。不幸的是,当发生这种情况时(点击播放按钮,在这种情况下是硬币)我在控制台中得到了大量的胡言乱语(至少对我来说)。所以我问你们这些是这个。为什么会发生这种情况?有哪些可能的解决方案?是否有任何调试技巧可用于破译我的控制台中出现的内容?继承人的“水平”:
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上的第一篇文章,因此非常感谢建设性的批评。
答案 0 :(得分:0)
与图形坐标相比,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);
}
}
}