2013-07-26 12 views
0

我正在製作一個基本的太空入侵者遊戲。我從LWJGL .zip文件中獲得了所有的資源(我不使用LWJGL librarys來創建我的遊戲,只是從中獲得了圖片等)。無論如何,只要按鍵盤上的「空格」,我的KeyListener就會創建一個新的我的船發射的子彈。但是,我不知道如何繪製子彈圖像,因爲我的KeyListener不傳遞一個圖形對象,並且你需要一個繪製圖像。導致問題的代碼是「Shot」構造函數中的「drawImage」方法。我的繼承人代碼:我如何獲得Java中的圖形對象?

public class KeyTyped{ 

    public void keyESC(){ 
     Screen.isRunning = false; 
    } 

    public void keyLEFT() { 
     Screen.shipPosX -= 10; 

    } 

    public void keyRIGHT() { 
     Screen.shipPosX += 10; 

    } 
    //This is automatically called from my KeyListener whenever i 
    //press the spacebar 
    public void keySPACE(){ 
     if(!spacePressed){ 
      ShotHandler.scheduleNewShot(); 
     }else{ 
      return; 
     } 
    } 


} 

公共類ShotHandler {

public static int shotX = Screen.shipPosX; 
public static int shotY = Screen.shipPosY + 25; 




public static void scheduleNewShot() { 
    //All this does is set a boolean to 'false', not allowing you to fire any more shots until a second has passed. 
    new ShotScheduler(1); 


    new Shot(25); 
} 

}

公共類射擊擴展ShotHandler {

public Shot(int par1){ 

    //This is my own method to draw a image. The first parameter is the graphics object that i need to draw. 
    GUI.drawImage(*????????*, "res/spaceinvaders/shot.gif", ShotHandler.shotX, ShotHandler.shotY); 

} 
      //Dont worry about this, i was just testing something 
    for(int i = 0; i <= par1; i++){ 
     ShotHandler.shotY++; 
    } 
} 

}

謝謝你們!任何幫助將不勝感激!

回答

1

您需要在某處存儲「子彈」的位置,然後在您的paintComponent(Graphics g)方法中訪問該狀態。真的,這應該是相當多的因素。讓你Shot類是這個樣子:

public class Shot { 
    private Point location; // Could be Point2D or whatever class you need 

    public Shot(Point initLocation) { 
     this.location = initLocation; 
    } 

    // Add getter and setter for location 

    public void draw(Graphics g) { 
     // put your drawing code here, based on location 
    } 
} 

然後在你按下鍵的方法,

public void keySPACE(){ 
    // Add a new shot to a list or variable somewhere 
    // WARNING: You're getting into multithreading territory. 
    // You may want to use a synchronized list. 
    yourJPanelVar.repaint(); 
} 

而且將擴展JPanel並覆蓋paintComponent

public class GameScreen extends JPanel { 
    public paintComponent(Graphics g) { 
     for (shot : yourListOfShots) { 
      shot.draw(g); 
     } 
     // And draw your ship and whatever else you need 
    } 
} 

這就是基本思想。希望它現在有一定意義。我想,你可以將Shot的繪圖代碼移動到其他地方,但爲了簡單起見,我只是堅持在Shot類本身。

我會注意到,我上面的是一些相當凌亂的代碼。看看MVC模式。它使狀態抽象更清晰(將狀態從顯示代碼中分離出來)。

+0

'foreach'在Java中不存在,它只是'for' – BackSlash

+0

@BackSlash謝謝。在C#上已經有一段時間了。 – jpmc26

5

這個想法是,聽衆應該改變遊戲的狀態(即創建一個子彈,或者改變子彈的位置,或者其他),然後調用repaint()。然後Swing將調用paintComponent(Graphics g)方法,該方法將使用Graphics作爲參數傳遞來繪製更新後的遊戲狀態。

+0

你能解釋一下嗎?我知道paintComponent(),repaint(),states等等,但我似乎不能把它放在一起。 (對不起,我剛開始看java遊戲教程,我知道Java至少2年了,但我不是最高級的遊戲程序員,但是xD。) – Geforce132

+0

如果您正在做一些自定義繪畫,請嘗試覆蓋'在你的'JPanel'上繪製(Graphics g)'方法(一定要調用'super.paint(g)'),並把渲染代碼放在那裏。您使用傳入的'Graphics'對象。然後,在調用'repaint()'時,您的自定義渲染將被觸發。 – akf

+0

@akf Swing類建議不要重寫'paint',我記得。 'paint'包含處理背景等的代碼。 – jpmc26

相關問題