麻烦高手帮忙看一下,为什么按空格后炮弹不发射了,再按空格炮弹还不发射。(我想的是按空格炮弹发射,再按不发射;按一下发射一次也行);小弟菜鸟,初学Java。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.lang.Math;
public class Pete extends JFrame
{
public enemytank enemy;
public enemytank1 enemy1;
public ammo ammo1;
public ammoo ammoo1;
public myammoo myammoo1;
public int i=0;
PetePanel pete = new PetePanel();
public Pete()
{
super("Tank Battle");
setSize(512, 430);
setResizable(false);
enemy=new enemytank(pete);
enemy1=new enemytank1(pete);
ammo1=new ammo(pete);
ammoo1=new ammoo(pete);
myammoo1=new myammoo(pete);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//添加窗口关闭
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(1, 1, 15, 15));
pane.add(pete);
this.addKeyListener(new KeyAdapter(){//得到焦点,放frame容易获取焦点。
@Override
public void keyPressed(KeyEvent e)
{
System.out.println("ppppp");
if (e.getKeyCode() == KeyEvent.VK_SPACE) //fire
{
i=i+1;
if(i%2==0)
pete.live=1;
else pete.live=0;
System.out.println(pete.live);
}
}
public void keyTyped(KeyEvent e)
{
System.out.println("kkkkk");
}
public void keyReleased(KeyEvent e)
{
System.out.println("rrrrrr");
}
});
setContentPane(pane);//建立联系与pane
enemy.start();
enemy1.start();
ammo1.start();
ammoo1.start();
myammoo1.start();
this.setVisible(true);//窗口可见
this.requestFocus();//具体办法可以手设焦点。如果焦点有问题,就优先想到它
}
public static void main(String[] arguments)
{
Pete Penguin = new Pete();
PetePanel a = new PetePanel();
}
}
class PetePanel extends JPanel{
Image petePics[] = new Image[4];
Image petePics1[] = new Image[4];
Image petePics2[] = new Image[4];
Image petePics3[] = new Image[4];
Image petePics4[] = new Image[4];
Image petePics5[] = new Image[4];
Image back;
int current = 0;
int x = 100;
int y = 100;
int current1 = 0;
int x1 = 300;
int y1 = 300;
int current2 = 0;
int x2 = 400;
int y2 = 50;
int current3 = 0;
int x3 = -20;
int y3 = -20;
int current4 = 0;
int x4 = -20;
int y4 = -20;
int current5 = 0;
int x5 = -20;
int y5 = -20;
int live=1;
PetePanel()
{
super();
String peteSrc[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
String peteSrc1[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
String peteSrc2[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
String peteSrc3[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
String peteSrc4[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
String peteSrc5[] = { "ar.jpg", "al.jpg", "au.jpg", "ad.jpg" };
Toolkit kit = Toolkit.getDefaultToolkit();
for (int i = 0; i < petePics.length; i++)