我正在製作一個基本的太空入侵者遊戲。我從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++;
}
}
}
謝謝你們!任何幫助將不勝感激!
'foreach'在Java中不存在,它只是'for' – BackSlash
@BackSlash謝謝。在C#上已經有一段時間了。 – jpmc26