我正在制作一个简单的太空入侵者型游戏,当我尝试使用计时器为向上发射的镜头制作动画时,我犯了一个错误。我是否应该修改我的代码,以便将镜头添加到单独的面板,然后将该面板添加到我的框架中的主面板?或者我可以按照我的方式保留它,只需清理代码并重新编写逻辑吗?
这是我完成的代码:
public static void main(String[] args) throws IOException {
GameTest t = new GameTest();
}
public static class GameTest extends JFrame {
private static final int WINDOW_WIDTH = 800;
private static final int WINDOW_HEIGHT = 500;
public static GamePanel gamePanel;
public GameTest() throws IOException {
super("Deep Fried Freedom");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setLayout(new BorderLayout());
gamePanel = new GamePanel();
add(gamePanel);
center(this);
setVisible(true);
this.addKeyListener(new aKeyListener());
this.setFocusable(true);
}
public void center(JFrame frame) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Point center = ge.getCenterPoint();
int w = frame.getWidth();
int h = frame.getHeight();
int x = center.x - w / 2, y = center.y - h / 2;
frame.setBounds(x, y, w, h);
frame.validate();
}//end of center method
public class aKeyListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
}//end empty keyTyped method
@Override
public void keyPressed(KeyEvent e) {
if (Launcher.lxCoord == 0 || Launcher.lxCoord == 735) { //ensure the launcher can't leave the frame
Launcher.lRun *= -1;
} else {
switch (e.getKeyCode()) {
case KeyEvent.VK_A : Launcher.lRun = -5; break;
case KeyEvent.VK_D : Launcher.lRun = 5; break;
case KeyEvent.VK_ENTER :
gamePanel.numShots++; gamePanel.createShots();
break;
default: Launcher.lRun = 0;
}
}
gamePanel.move(gamePanel);
}//end keyPressed method
@Override
public void keyReleased(KeyEvent e) {
}//end empty keyReleased method
}//end aKeyListener class
}//end GameTest class
}// end main class
public class GamePanel extends JPanel {
Launcher launcher1;
Background bground1;
public static ArrayList<Shot> shots;
public int numShots;
public static int counter;
public GamePanel() throws IOException {
super();
this.shots = new ArrayList<>();
this.numShots = 0;
launcher1 = new Launcher();
bground1 = new Background();
}//end constructor
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bground1.background, 0, 0, getWidth(), getHeight(), null);
g.drawImage(launcher1.baldEagleImage, launcher1.getLxCoord(), launcher1.lyCoord, null);//paint the launcher
while (counter == 1) {
for (int i = 0; i < numShots; i++) {
g.drawImage(shots.get(i).mcDShotImage, shots.get(i).staticXLauncherCoord, shots.get(i).getSyCoord(), null);
}
}
}//end paintComponent method
public void move(GamePanel gamePanel) {
launcher1.moveX();
if (numShots > 0) {
moveShot();
}
repaint();
}//end move method
public void moveShot() {
for (int i = 0; i < numShots; i++) {//loop to move all the shots
if (shots.get(i).getSyCoord() > 10) {
counter = 1;
shots.get(i).moveY();
repaint();
} else {
counter = 0;
numShots--;
shots.remove(i);
repaint();
}
}
}//end shot method
public void createShots() {
try {
for (int j = 0; j < numShots; j++) {
shots.add(new Shot());
}
} catch (IOException | IndexOutOfBoundsException e) {
System.out.println("caught an exception" + e);
}
}
}//end GamePanel class
public class Shot {
public int syCoord;
public int sRise = 5;
public BufferedImage mcDShotImage;
GamePanel gPanel;
public static int staticXLauncherCoord;
private Timer timer;
public Shot() throws IOException {
timer = new Timer(20, new TimerListener());
staticXLauncherCoord = Launcher.getLxCoord() + 10;
syCoord = 381;
mcDShotImage = ImageIO.read(new File("mcdonaldsarchesshot.jpg"));
}//end constructor
public void moveY() {
syCoord -= sRise;
setSyCoord(syCoord);
}//end moveY method
public void setSyCoord(int syCoord) {
this.syCoord = syCoord;
}
public int getSyCoord() {
return this.syCoord;
}
public class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < GamePanel.shots.size(); i++) {
moveY();
}
}//end actionPerformed method
}//end TimerListener class
}//end Shot class
public class Launcher {
public static int lxCoord; //the launcher's x coordinate
public static final int lyCoord = 415;
public static int lRun = 0; //the launcher's x change
public static BufferedImage baldEagleImage;
//Constructor
public Launcher() throws IOException {
lxCoord = 350;
baldEagleImage = ImageIO.read(new File("baldeagleimage.jpg"));
}
/**
* The movement of the launcher in the x direction
*/
public void moveX() {
lxCoord += lRun;
setLxCoord(lxCoord);
}//end moveX method
public void setLxCoord(int lxCoord) {
this.lxCoord = lxCoord;
}
public static int getLxCoord() {
return lxCoord;
}
}//end Launcher class
答案 0 :(得分:1)
while (counter == 1) {
中的 paintComponent
将阻止事件调度线程处理可能添加到事件队列的任何新事件。基本上,这意味着counter
变量根本不可能再次设置为0
...
Timer
中的Shot
永远不会开始。
更改代码,以便......
Timer
,其职责是更新游戏模型并触发repaint
paintComponent
只是在调用时绘制当前帧/状态,然后立即存在