java中键盘按键事件没有响应,鼠标移动事件有响应,不知道问题在哪
publicvoidaction(){//键盘操作KeyListenerk=newKeyAdapter(){publicvoidkeyPressed(KeyEvente)...
public void action(){
//键盘操作
KeyListener k =new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if(state==RUNNING){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:hero.moveLeft();break;
case KeyEvent.VK_RIGHT:hero.moveRight();break;
case KeyEvent.VK_UP:hero.moveUp();break;
case KeyEvent.VK_DOWN:hero.moveDown();break;
}
}
}
};
this.addKeyListener(k);
MouseAdapter l = new MouseAdapter(){
public void mouseMoved(MouseEvent e){
if(state==RUNNING){
int x = e.getX();
int y = e.getY();
hero.moveTo(x, y);
}
}
public void mouseClicked(MouseEvent e){
if(state==START){
state=RUNNING;
}
if(state==GAMEOVER){
score=0;
hero = new Hero();
flyings=new FlyingObject[0];
bullets=new Bullet[0];
enemybullets =new EnemyBullet[0];
state=START;
}
}
public void mouseExited(MouseEvent e){
if(state ==RUNNING){
state=PAUSE;
}
}
public void mouseEntered(MouseEvent e){
if(state==PAUSE){
state=RUNNING;
}
}
};
this.addMouseMotionListener(l); // 添加鼠标移动事件
this.addMouseListener(l); // 添加鼠标操作事件
Timer timer = new Timer();
int intervel = 10;
timer.schedule(new TimerTask(){
public void run(){
if(state==RUNNING){
enemyEnter();
stepAction();
bulletEnter();
enemyBulletEnter();
deleteOutOfBounds();
bangAction();
bulletBangAction();
enemybuletHitHero();
hitAction();
}
repaint();
}
}, intervel,intervel);
}
public static void main(String[] args) {
JFrame frame = new JFrame("飞机大战");
FlyGame game = new FlyGame();
frame.add(game);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.action();
} 展开
//键盘操作
KeyListener k =new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if(state==RUNNING){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:hero.moveLeft();break;
case KeyEvent.VK_RIGHT:hero.moveRight();break;
case KeyEvent.VK_UP:hero.moveUp();break;
case KeyEvent.VK_DOWN:hero.moveDown();break;
}
}
}
};
this.addKeyListener(k);
MouseAdapter l = new MouseAdapter(){
public void mouseMoved(MouseEvent e){
if(state==RUNNING){
int x = e.getX();
int y = e.getY();
hero.moveTo(x, y);
}
}
public void mouseClicked(MouseEvent e){
if(state==START){
state=RUNNING;
}
if(state==GAMEOVER){
score=0;
hero = new Hero();
flyings=new FlyingObject[0];
bullets=new Bullet[0];
enemybullets =new EnemyBullet[0];
state=START;
}
}
public void mouseExited(MouseEvent e){
if(state ==RUNNING){
state=PAUSE;
}
}
public void mouseEntered(MouseEvent e){
if(state==PAUSE){
state=RUNNING;
}
}
};
this.addMouseMotionListener(l); // 添加鼠标移动事件
this.addMouseListener(l); // 添加鼠标操作事件
Timer timer = new Timer();
int intervel = 10;
timer.schedule(new TimerTask(){
public void run(){
if(state==RUNNING){
enemyEnter();
stepAction();
bulletEnter();
enemyBulletEnter();
deleteOutOfBounds();
bangAction();
bulletBangAction();
enemybuletHitHero();
hitAction();
}
repaint();
}
}, intervel,intervel);
}
public static void main(String[] args) {
JFrame frame = new JFrame("飞机大战");
FlyGame game = new FlyGame();
frame.add(game);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.action();
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询