2014-02-18 52 views
0

好的這段代碼,我有一個可愛的小遊戲扔在一起。在理論上,level.java應該啓動Menu.java,並且一旦在屏幕上單擊播放按鈕,您應該轉換到Playing.java狀態。不幸的是,當發生這種情況時(點擊播放按鈕,在這種情況下是硬幣),我的控制檯中會有大量的亂碼(至少對我來說)。所以我問你們是這樣的。爲什麼會發生這種情況?什麼是可能的解決方案?是否有任何調試技巧來解密我的控制檯中出現的內容?繼承人的「級別」:使用slick2D有困難的移動狀態

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(); 
}}} 

這裏是「菜單」和PLAY鍵:

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

與圖形座標相比,Slick2d中的鼠標座標相反。例如:

當您處於(0,0)時,鼠標Y將等於您的屏幕高度。我希望這能解決你的問題。

編輯

在進一步研究,我發現,在演奏類COIN2沒有分配給它的圖像,這是因爲空指針異常的崩潰也就是你需要指定像其他的圖像那些。

這裏是更新鼠標座標(並且設置爲圖像座標的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); 
    } 
    } 

} 
+0

特別是在哪種情況的「播放」按鈕的點擊來傳輸?如果是這樣,我想這就是解決方案 – user2867659

+0

我更新了我的答案,包括更多的細節獲取更新的座標,Y座標仍然倒置 – Hayden

+0

從頭開始,當我放入更新的部分時,所做的只是移動座標以匹配它們應該如何與slick2D相匹配(正如你所說的向右翻轉?),點擊按鈕應該在的新區域後,我會得到同樣的崩潰。 – user2867659