0
我正在嘗試使用Java創建我的第一個遊戲。我有我的畫布和一個可以移動的小圈子玩家。當我按下左箭頭鍵時,角色向上和向左移動。當我按右時,角色向下移動到右側。上下箭頭什麼都不做。我已經檢查了我是否有任何變數混淆,但我認爲我沒有。我是很新,所以如果任何人都可以告訴我爲什麼和在哪裏出了錯,那將是沒什麼太大的幫助按左或右箭頭鍵時對角移動字符
我Game.java是這樣的:
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
public class GamePanel extends JPanel implements Runnable, KeyListener{
// Fields
public static int WIDTH = 600;
public static int HEIGHT = 600;
private Thread thread;
private boolean running;
private BufferedImage image;
private Graphics2D g;
private int FPS = 30;
private double averageFPS;
private Player player;
// Constructor
public GamePanel() {
super();
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
requestFocus();
}
// Functions
public void addNotify() {
super.addNotify();
if (thread == null) {
thread = new Thread(this);
thread.start();
}
addKeyListener(this);
}
public void run() {
running = true;
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
player = new Player();
long startTime;
long URDTimeMillis;
long waitTime;
long totalTime = 0;
int frameCount = 0;
int maxFrameCount = 30;
long targetTime = 1000/FPS;
// Game Frame Limiter and Timer
while (running) {
startTime = System.nanoTime();
gameUpdate();
gameRender();
gameDraw();
URDTimeMillis = (System.nanoTime() - startTime)/1000000;
waitTime = targetTime - URDTimeMillis;
try {
Thread.sleep(waitTime);
} catch (Exception ignored){
}
totalTime += System.nanoTime() - startTime;
frameCount ++;
if (frameCount == maxFrameCount) {
averageFPS = 1000.0/((totalTime/frameCount)/1000000);
frameCount = 0;
totalTime = 0;
}
}
}
private void gameUpdate() {
player.update();
}
private void gameRender() {
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.drawString("FPS: " + averageFPS, 10, 10);
player.draw(g);
}
private void gameDraw() {
Graphics g2 = this.getGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
}
public void keyTyped(KeyEvent key){
}
public void keyPressed(KeyEvent key){
int keyCode = key.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT){
player.setLeft(true);
}
if (keyCode == KeyEvent.VK_RIGHT){
player.setRight(true);
}
if (keyCode == KeyEvent.VK_UP){
player.setUp(true);
}
if (keyCode == KeyEvent.VK_DOWN){
player.setDown(true);
}
}
public void keyReleased(KeyEvent key){
int keyCode = key.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT){
player.setLeft(false);
}
if (keyCode == KeyEvent.VK_RIGHT){
player.setRight(false);
}
if (keyCode == KeyEvent.VK_UP){
player.setUp(false);
}
if (keyCode == KeyEvent.VK_DOWN){
player.setDown(false);
}
}
}
這是我Player.java
import java.awt.*;
public class Player {
// Fields
private int x;
private int y;
private int r;
private int dx;
private int dy;
private int speed;
private boolean left;
private boolean right;
private boolean up;
private boolean down;
private int lives;
private Color color1;
private Color color2;
// Construtor
public Player(){
x = GamePanel.WIDTH /2;
y = GamePanel.HEIGHT /2;
r = 5;
dx = 0;
dy = 0;
speed = 5;
lives = 3;
color1 = Color.WHITE;
color2 = Color.BLACK;
}
// Functions
public void setLeft(boolean b){left = b;}
public void setRight(boolean b){right = b;}
public void setUp(boolean b){up = b;}
public void setDown(boolean b){down = b;}
public void update(){
if (left){
dx = -speed;
}
if (right){
dx = speed;
}
if (up){
dy = speed;
}
if (down){
dy = -speed;
}
x += dx;
y += dy;
if (x < r){
x = r;
}
if (y < r){
y = r;
}
if (x > GamePanel.WIDTH - r){
x = GamePanel.WIDTH - r;
}
if (y > GamePanel.HEIGHT - r){
y = GamePanel.HEIGHT - r;
}
dx = 0;
dy = 0;
}
public void draw(Graphics2D g){
g.setColor(color1);
g.fillOval(x - r, x - r, 2 * r, 2 * r);
g.setStroke(new BasicStroke(3));
g.setColor(color1.darker());
g.drawOval(x - r, x - r, 2 * r, 2 * r);
g.setStroke(new BasicStroke(1));
}
}
也許試着在Player.update()結束時將玩家的'left','right'等成員重置爲'false'而不是依賴'keyReleased()'回調。只是爲了確保問題不僅僅是密鑰版本沒有正確註冊。 – domsson
可能重複的[如何使用密鑰綁定而不是密鑰監聽器](http://stackoverflow.com/questions/22741215/how-to-use-key-bindings-instead-of-key-listeners) – user1803551
@domdom我嘗試了這一點,它只是讓玩家反應遲鈍(移動速度較慢),但它仍然向上和向左,向下和向右移動。上下箭頭什麼都不做 –