2013-07-26 13 views
-1

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

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++; 
    } 
} 

}

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

(這是一個重新職位,因爲我最後一次張貼了這個,我沒有得到足夠的答案,至少在我的技能等級)在

+0

'yourpanel。的getGraphics()'? – BackSlash

+0

我試過了,但它沒有繪製圖片。我不知道爲什麼。 – Geforce132

+2

因爲,正如@JBNizet在前面的問題中告訴你的,這是**不是**的正確方法。 _這是一個重新發布,因爲上次我發佈這個,我沒有得到足夠的答案,至少對我的技能水平_你在1小時前問你以前的問題。你不覺得你至少可以等一天讓所有其他用戶看到並回答你的問題嗎? – BackSlash

回答

3

開始通過查看

你也可以找到一些有用的Concurrency in Swing

基本要點是你應該有某種模型來描述遊戲在任何時刻的狀態。此模型在事件派發線程的內容之外更新,通常在某種後臺線程中執行更新所需的時間不會影響Swing繼續運行並保持對用戶的響應能力。

您將需要覆蓋JPanelpaintComponent方法。在這裏,您可以將模型狀態繪製到屏幕上。

有一堆可能討論其他技術,但讓趕快開始

例子...