我正在制作一个基本的太空入侵者游戏。我从 LWJGL .zip 文件中获得了所有资源(我没有使用 LWJGL 库来创建我的游戏,只是从中获得了图片等。)无论如何,每当我在键盘上按“空格”时,我的 KeyListener 都会创建一个新的我的船发射的子弹。但是,我不知道如何绘制子弹图像,因为我的 KeyListener 没有传递图形对象,而您需要一个来绘制图像。导致问题的代码是“Shot”构造函数中的“drawImage”方法。这是我的代码:
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);
}
}
公共类 Shot 扩展 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++;
}
}
}
多谢你们!任何帮助将不胜感激!
(这是一个重新发布,因为我上次发布此内容时,我没有得到足够的答案,至少对于我的技能水平而言)