出于某种原因,关键输入在mac计算机上停止工作一段时间后。我把我的项目发送给了我的朋友,他们说它完美无缺,但一段时间后关键输入打破了。这有什么理由吗?它看起来好像是无处不在,通常发生在我移动玩家之后。它似乎也没有模式。我也没有收到任何错误消息或崩溃消息,所以我真的很困惑。感谢所有帮助:D
package entity;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import main.Animation;
import main.Sprite;
public class Player {
KeyListener keyboard;
Animation animation;
Animation walkRight;
Animation walkLeft;
Animation walkUp;
Animation walkDown;
Animation stand;
Sprite sprite;
// BufferedImage currentSprite;
boolean walkY, walkX;
private int scale, x, y, tick;
private boolean right, left, up, down, middleOfRight, middleOfLeft, middleOfUp, middleOfDown;
public Player(int scale) {
initKeyboard();
sprite = new Sprite("spriteSheet", 32);
walkLeft = new Animation(sprite, 0, 2, 1, 1, 16, true);
walkRight = new Animation(sprite, 0, 2, 2, 2, 16, true);
walkUp = new Animation(sprite, 0, 2, 3, 3, 16, true);
walkDown = new Animation(sprite, 0, 2, 0, 0, 16, true);
stand = new Animation(sprite, 1, 1, 0, 0, 10000, false);
animation = stand;
this.scale = scale;
x = 32;
y = 32;
}
public void update() {
animation.update();
if (right || middleOfRight) {
x += 2;
if (x % 32 == 0) {
middleOfRight = false;
if (!right && animation != walkUp && animation != walkDown) {
animation = stand;
}
} else
middleOfRight = true;
} else if (left || middleOfLeft) {
x -= 2;
if (x % 32 == 0) {
middleOfLeft = false;
if (!left && animation != walkUp && animation != walkDown) {
animation = stand;
}
} else
middleOfLeft = true;
}
if (up || middleOfUp) {
y -= 2;
if (y % 32 == 0) {
middleOfUp = false;
if (!up && animation != walkRight && animation != walkLeft) {
animation = stand;
}
} else
middleOfUp = true;
} else if (down || middleOfDown) {
y += 2;
if (y % 32 == 0) {
middleOfDown = false;
if (!down && animation != walkRight && animation != walkLeft) {
animation = stand;
}
} else
middleOfDown = true;
}
tick++;
}
public void render(Graphics screen) {
screen.drawImage(animation.getSprite(), x, y, 32 * scale, 32 * scale, null);
}
public KeyListener getKeyboard() {
return keyboard;
}
private void initKeyboard() {
keyboard = new MyKeyListener();
}
class MyKeyListener extends KeyAdapter {
private boolean[] keysX = { false, false };
private boolean[] keysY = { false, false };
public void keyPressed(KeyEvent e) {
System.out.println("test");
int key = e.getKeyCode();
switch (key) {
// case (KeyEvent.VK_RIGHT):
case (KeyEvent.VK_D):
right = true;
left = false;
middleOfLeft = false;
animation = walkRight;
animation.start();
tick = 0;
break;
// case (KeyEvent.VK_LEFT):
case (KeyEvent.VK_A):
right = false;
left = true;
middleOfRight = false;
animation = walkLeft;
animation.start();
tick = 0;
break;
// case (KeyEvent.VK_DOWN):
case (KeyEvent.VK_S):
up = false;
down = true;
middleOfUp = false;
if (animation == stand || animation == walkUp) {
animation = walkDown;
animation.start();
}
break;
// case (KeyEvent.VK_UP):
case (KeyEvent.VK_W):
down = false;
up = true;
middleOfDown = false;
if (animation == stand || animation == walkDown) {
animation = walkUp;
animation.start();
}
break;
}
}
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case (KeyEvent.VK_W):
up = false;
break;
case (KeyEvent.VK_S):
down = false;
break;
case (KeyEvent.VK_A):
left = false;
if (up) {
animation = walkUp;
animation.start();
} else if (down) {
animation = walkDown;
animation.start();
}
break;
case (KeyEvent.VK_D):
right = false;
if (up) {
animation = walkUp;
animation.start();
} else if (down) {
animation = walkDown;
animation.start();
}
break;
}
}
}
public void setUpInput(JFrame frame) {
JPanel pane = (JPanel) frame.getContentPane();
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("D"), "move right");
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released D"), "release right");
pane.getActionMap().put("move right", new MoveAction("right", false));
pane.getActionMap().put("release right", new MoveAction("right", true));
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("A"), "move left");
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released A"), "release left");
pane.getActionMap().put("move left", new MoveAction("left", false));
pane.getActionMap().put("release left", new MoveAction("left", true));
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "move up");
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released W"), "release up");
pane.getActionMap().put("move up", new MoveAction("up", false));
pane.getActionMap().put("release up", new MoveAction("up", true));
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"), "move down");
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released S"), "release down");
pane.getActionMap().put("move down", new MoveAction("down", false));
pane.getActionMap().put("release down", new MoveAction("down", true));
}
private class MoveAction extends AbstractAction {
private static final long serialVersionUID = 1L;
String direction;
boolean released;
MoveAction(String direction, boolean released) {
this.direction = direction;
this.released = released;
}
@Override
public void actionPerformed(ActionEvent e) {
if (!released) {
switch (direction) {
// case (KeyEvent.VK_RIGHT):
case ("right"):
right = true;
left = false;
middleOfLeft = false;
animation = walkRight;
animation.start();
tick = 0;
break;
// case (KeyEvent.VK_LEFT):
case ("left"):
right = false;
left = true;
middleOfRight = false;
animation = walkLeft;
animation.start();
tick = 0;
break;
// case (KeyEvent.VK_DOWN):
case ("down"):
up = false;
down = true;
middleOfUp = false;
if (animation == stand || animation == walkUp) {
animation = walkDown;
animation.start();
}
break;
// case (KeyEvent.VK_UP):
case ("up"):
down = false;
up = true;
middleOfDown = false;
if (animation == stand || animation == walkDown) {
animation = walkUp;
animation.start();
}
break;
}
} else {
switch (direction) {
case ("up"):
up = false;
break;
case ("down"):
down = false;
break;
case ("left"):
left = false;
if (up) {
animation = walkUp;
animation.start();
} else if (down) {
animation = walkDown;
animation.start();
}
break;
case ("right"):
right = false;
if (up) {
animation = walkUp;
animation.start();
} else if (down) {
animation = walkDown;
animation.start();
}
break;
}
}
}
}
}