简体   繁体   English

如何比其他方法同时运行KeyListener?

[英]How to run a KeyListener at the same time than the other methods?

I'm actually coding pacman for a project, but I've encountered an issue, when I've added this part : 我实际上是为一个项目编码pacman,但是在添加此部分时遇到了一个问题:

@Override
public void keyPressed(KeyEvent e){
    dir=e.getKeyCode();         
}

The program doesn't call that specific method that I need in order to get the right direction and then put it in the update one. 该程序没有调用我需要的特定方法,以便获得正确的方向,然后将其放入更新程序中。

Here is the full code which I'm still working on : 这是我仍在处理的完整代码:

import java.awt.Graphics;
import org.game.engine.Game;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.game.engine.GameApplication;

public class Pacman extends Game  {
BufferedImage pacman;
int frame;
int dir;
int x,y;
final int STEP=2;

public static void main (String[] args){
    GameApplication.start(new Pacman());
}

public Pacman(){
    title="Pacman";
    width=height=500;
    dir=KeyEvent.VK_RIGHT;
    x=300;
    y=200;
    try {
        pacman = ImageIO.read(new File("pacman.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void keyPressed(KeyEvent e){
    dir=e.getKeyCode();                
}

@Override
public void update() {
    frame++;
    if(frame>2){
        frame=0;
    }
    **switch(dir){
        case KeyEvent.VK_LEFT:
            x-=STEP;
            break;
        case KeyEvent.VK_RIGHT:
            x+=STEP;
            break;**
    }

    if(x<0){
        x=0;
    }
    else if(x> width -28-15){
        x= width-28-15;
    }

}

@Override
public void draw(Graphics g) {
    g.drawImage(pacman.getSubimage(frame*30,0,28,28),x,y,null);
}

@Override
public void init() {
}

} }

EXTRA Details : So basically in order to run the project I've a game engine that helps me to process the shape of the game. 额外详情:因此,基本上,为了运行项目,我有一个游戏引擎,可以帮助我处理游戏的形状。 In order to do that I have 4 classes : 为此,我有4个课程:

1/ Game Application : 1 /游戏应用程序:

package org.game.engine;

import javax.swing.JFrame;

public class GameApplication {
static public void start (Game jeu) {
    JFrame fenetre=new JFrame(jeu.getTitle());
    fenetre.setSize(jeu.getWidth(),jeu.getHeight());
    fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GameCanvas canvas=new GameCanvas(jeu);
    fenetre.add(canvas);
    fenetre.setVisible(true);
    GameLoop loop= new GameLoop(jeu,canvas);
    loop.start();
 }
}

2/ GameCanvas 2 / GameCanvas

package org.game.engine;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; 
import javax.swing.JComponent;

public class GameCanvas extends JComponent{
private final Game game;

public GameCanvas(Game game) {
    this.game=game;
    **addKeyListener(this.game);** 
    requestFocus();
}
@Override
public void paintComponent(Graphics g){
    game.draw(g);
}

}

3/ Game Loop 3 /游戏循环

package org.game.engine;
public class GameLoop extends Thread {
   private final Game game;
   private final GameCanvas canvas;

  public GameLoop(Game game, GameCanvas canvas) {
    this.game=game;
    this.canvas=canvas; 
  }

@Override
public void run() {
    game.init();
    while(!game.isOver()){
        game.update();
        canvas.repaint();
        try {
            Thread.sleep(game.getDelay());
        } catch (InterruptedException e) {
        }
     }
  }
 }

And finally the game itself : 最后是游戏本身:

4/ Game 4 /游戏

package org.game.engine;
import java.awt.Graphics;
**import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;**

public abstract class Game **implements KeyListener**{
protected boolean over;
protected int delay=50;
protected int width=500;
protected int height=500;
protected String title="Mon jeu";

abstract public void update();
abstract public void draw(Graphics g);
abstract public void init();

public int getWidth(){ return width;}
public int getHeight(){return height;}
public String getTitle(){return title;}

public boolean isOver(){ return over;}
public long getDelay(){ return delay;}

**public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}**
}

The 1/ Game Application is for the execution part of the process (layout display etc..) The 2/ Game Canvas is there in order to draw the content of the window, besides it also calls the Keylistener by : addKeyListener(this.game); 1 /游戏应用程序用于流程的执行部分(布局显示等)。2 /游戏画布用于绘制窗口的内容,此外它还通过以下方式调用Keylistener:addKeyListener(this.game ); The 3/ Game Loop contains the main loop of the programm in the run method And finally the 4/ Game contains all the basic parametres of the programm such as the title the size and it also implements the Keylisteners. 3 / Game循环包含run方法中程序的主循环,最后4 / Game包含程序的所有基本参数,例如标题的大小, 并且还实现了Keylistener。

For key press to be handled correctly you need to do following: 为了正确处理按键,您需要执行以下操作:

  • Register the key listener/ key adapter to Swing (or AWT). 将密钥侦听器/密钥适配器注册到Swing(或AWT)。
  • After key pressed event is called call the method that should respond to the key. 调用按键事件后,调用应响应按键的方法。

May be you are doing these right. 可能是您做对了。 If so you need to narrow down the problem further for getting reasonable help. 如果是这样,您需要进一步缩小问题范围,以获得合理的帮助。

You're storing the key value in a and then checking dir in your switch. 你存储在键值a ,然后检查dir中的交换机。 Obviously this will not work. 显然这是行不通的。 You do not seem to use a anywhere else in the application so I suggest you remove the variable completely to avoid confusion. 您似乎没有在应用程序中a任何其他地方使用该文件,因此建议您完全删除该变量,以免造成混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何同时运行ActionListener和KeyListener? - How to run ActionListener and KeyListener at the same time? 如何允许多个方法同时运行,而某些其他方法没有运行? - How can I allow multiple methods to run at the same time, while certain other methods are not running? 如何同时使用Keylistener和Timer? - how to use keylistener and timer at the same time? 同时使用MouseListener和KeyListener - MouseListener and KeyListener used at the same time 如何使用 KeyListener 同时检测多个键? - How to detect multiple keys at the same time using KeyListener? 两种方法实现相同的算法,一种在java中比其他方法花费更多的运行时间 - Two methods implement same algorithm one is taking running time more than other in java 如何同时运行/显示多个图形方法? - How do I run/display mutliple graphics methods at the same time? 如何使多个Android AsyncTask同时运行? - How to make more than one Android AsyncTask run at the same time? java中实现runnable的类是否可以使用run()以外的方法? - Can classes in java that implement runnable have methods other than run()? 是否可以在运行时而不是编译时进行验证检查? - Is it possible to to do validation checking at run time other than compile time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM