[英]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: 为了正确处理按键,您需要执行以下操作:
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.