我正在浏览如何创建太空飞船游戏的教程。我试图控制船射击导弹的速度。现在,当我按住空格按钮时,它会快速射击,因为keyPressed方法不断被调用。我有一个如下所示的keyboardInput类:
package com.game.src.main;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class KeyInput extends KeyAdapter {
Game game;
public KeyInput(Game game) {
this.game = game;
}
public void keyPressed(KeyEvent e) {
game.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
game.keyReleased(e);
}
}
如您所见,然后将关键事件传递给Game实例的方法。我已经设置了一个名为is_shooting的布尔值并将其设置为false。然后当按下spacekey时,它将检查is_shooting是否为false,如果是,则将其设置为true并制作导弹。当它被释放时,is_shooting会变回false。我在init()方法中创建了一个addkeyListener,这是线程开始运行时调用的第一个函数。我在我的游戏课上:
编辑添加了我的Game类的全部代码。
package com.game.src.main;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final int WIDTH = 320;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 2;
public final String TITLE = "2D Space Game";
private boolean running = false ;
private Thread thread;
private BufferedImage spriteSheet = null;
private BufferedImage background = null;
private boolean is_shooting = false;
private Player p;
private Controller c;
public void init(){
BufferedImageLoader loader = new BufferedImageLoader();
try{
spriteSheet = loader.loadImage("/sprite_sheet.png");
background = loader.loadImage("/background.png");
}catch(IOException e){
e.printStackTrace();
}
addKeyListener(new KeyInput(this));
p = new Player(200,200,this);
c = new Controller(this);
}
private synchronized void start(){
if (running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop(){
if (!running)
return;
running = false;
try{
thread.join();
}catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(1);
}
public void run(){
init();
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000/ amountOfTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while (running){
// this would be the game loop
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1 ){
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println(updates + "Ticks, FPS"+ frames);
updates = 0 ;
frames = 0;
}
}
stop();
}
private void tick(){
p.tick();
c.tick();
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if (bs ==null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(background,0,0,null);
p.render(g);
c.render(g);
g.dispose();
bs.show();
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT){
p.setVelX(5);
}
else if (key == KeyEvent.VK_LEFT){
p.setVelX(-5);
}
else if (key == KeyEvent.VK_DOWN){
p.setVelY( 5);
}
else if (key == KeyEvent.VK_UP){
p.setVelY(- 5);
}
else if (key == KeyEvent.VK_SPACE && !is_shooting)
{
is_shooting = true;
c.addBullet(new Bullet(p.getX(), p.getY(),this));
}
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT){
p.setVelX(0);
}
else if (key == KeyEvent.VK_LEFT){
p.setVelX(0);
}
else if (key == KeyEvent.VK_DOWN){
p.setVelY(0);
}
else if (key == KeyEvent.VK_UP){
p.setVelY(0);
System.out.println("IM in HEREE");
}
else if (key == KeyEvent.VK_SPACE){
is_shooting = false;
}
}
public static void main(String args[]){
Game game= new Game();
game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE ));
game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE ));
game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE ));
JFrame frame = new JFrame(game.TITLE);
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println(System.getProperty("user.dir"));
game.start();
}
public BufferedImage getSpriteSheet(){
return spriteSheet;
}
}
对于一些println语句,我发现keyReleased函数被调用了我正在按下的'ANY'键。当我按下键而不释放键时,如何防止调用keyReleased方法?