Java 按下绑定键时不断移动精灵
Java Move Sprite Constantly While Key Binding is Pressed
我创建了一个程序来创建一个正方形并将其绘制在 JPanel 上。还有控制方块移动的KeyBindings(w在上)(s在下)(a在左)(d在右)。我的问题是,当我按下键时,它移动了一个实例,犹豫了一下,然后继续移动。有什么办法可以防止运动的迟疑。
import javax.swing.JFrame;
public class MovingBox extends JFrame{
Panel panel;
public static void main(String args[]){
MovingBox mb = new MovingBox();
}
public MovingBox(){
super("Moving Box");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new Panel();
add(panel);
setVisible(true);
}
}
这是我的面板 class。
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Panel extends JPanel{
Rectangle box;
public Panel(){
box = new Rectangle(100, 100, 50, 50);
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "move left");
getActionMap().put("move left", new MoveAction(3, 3));
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "move right");
getActionMap().put("move right", new MoveAction(1, 3));
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "move up");
getActionMap().put("move up", new MoveAction(0, 3));
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "move down");
getActionMap().put("move down", new MoveAction(2, 3));
}
public void paintComponent(Graphics window){
super.paintComponent(window);
window.fillRect((int)box.getX(), (int)box.getY(), (int)box.getWidth(), (int)box.getHeight());
}
private class MoveAction extends AbstractAction{
int direction;
int speed;
public MoveAction(int direction, int speed){
this.direction = direction;
this.speed = speed;
}
public void actionPerformed(ActionEvent e){
if(direction == 0){
box.setLocation((int)box.getX(), (int)box.getY() - speed);
}else if(direction == 1){
box.setLocation((int)box.getX() - speed, (int)box.getY());
}else if(direction == 2){
box.setLocation((int)box.getX(), (int)box.getY() + speed);
}else if(direction == 3){
box.setLocation((int)box.getX() + speed, (int)box.getY());
}
repaint();
}
}
}
任何帮助将不胜感激。谢谢。
要获得平滑的运动,您需要创建一个线程来处理运动。
为每个键 (w, a, s, d) 创建一个布尔变量,并在按下键时将其设置为 true,在释放键时将其设置为 false。只是一个简单的关键监听器。
别忘了给jframe加上监听,检查jframe是否有焦点
private class InputListener implements KeyListener{
@Override
public void keyPressed(KeyEvent pressed) {
//arrow keys
if(pressed.getKeyCode() == 37){
arrowLeft = true;
}else if(pressed.getKeyCode() == 38){
arrowUp = true;
}else if(pressed.getKeyCode() == 39){
arrowRight = true;
}else if(pressed.getKeyCode() == 40){
arrowDown = true;
}
//System.out.println(pressed.getKeyCode());
}
@Override
public void keyReleased(KeyEvent released) {
if(released.getKeyCode() == 37){
arrowLeft = false;
}
if(released.getKeyCode() == 38){
arrowUp = false;
}
if(released.getKeyCode() == 39){
arrowRight = false;
}
if(released.getKeyCode() == 40){
arrowDown = false;
}
}
@Override
public void keyTyped(KeyEvent arg0) {}
}
我用它来用箭头键移动,所以对于 wasd 移动你需要改变键码。您需要的第二件事是在按下某个键时移动正方形的后台线程。它根据经过的速度和时间计算运动,因此非常流畅。
long currTime = 0;
long lastTime = 0;
new Thread() {
@Override
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
currTime = System.currentTimeMillis();
long time = currTime - lastTime;
int movePixel = (int) (speed * time);
if (movePixel < 1 ) {
currPlayerTime -= time;
}else{
if(arrowLeft){
//move left by movePixel pixel
}else if(arrowRight){
//move right by movePixel pixel
}
if(arrowUp){
}else if(arrowDown){
}
}
lastTime = currTime;
}
}
}.start();
这可能不是最简单和最快的解决方案,但绝对是最好的:)
Is there a way to prevent to the hesitation of the movement.
您可以使用 Swing Timer 来安排动画。查看 Motion Using the Keyboard 的工作示例。
我创建了一个程序来创建一个正方形并将其绘制在 JPanel 上。还有控制方块移动的KeyBindings(w在上)(s在下)(a在左)(d在右)。我的问题是,当我按下键时,它移动了一个实例,犹豫了一下,然后继续移动。有什么办法可以防止运动的迟疑。
import javax.swing.JFrame;
public class MovingBox extends JFrame{
Panel panel;
public static void main(String args[]){
MovingBox mb = new MovingBox();
}
public MovingBox(){
super("Moving Box");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new Panel();
add(panel);
setVisible(true);
}
}
这是我的面板 class。
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Panel extends JPanel{
Rectangle box;
public Panel(){
box = new Rectangle(100, 100, 50, 50);
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "move left");
getActionMap().put("move left", new MoveAction(3, 3));
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "move right");
getActionMap().put("move right", new MoveAction(1, 3));
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "move up");
getActionMap().put("move up", new MoveAction(0, 3));
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "move down");
getActionMap().put("move down", new MoveAction(2, 3));
}
public void paintComponent(Graphics window){
super.paintComponent(window);
window.fillRect((int)box.getX(), (int)box.getY(), (int)box.getWidth(), (int)box.getHeight());
}
private class MoveAction extends AbstractAction{
int direction;
int speed;
public MoveAction(int direction, int speed){
this.direction = direction;
this.speed = speed;
}
public void actionPerformed(ActionEvent e){
if(direction == 0){
box.setLocation((int)box.getX(), (int)box.getY() - speed);
}else if(direction == 1){
box.setLocation((int)box.getX() - speed, (int)box.getY());
}else if(direction == 2){
box.setLocation((int)box.getX(), (int)box.getY() + speed);
}else if(direction == 3){
box.setLocation((int)box.getX() + speed, (int)box.getY());
}
repaint();
}
}
}
任何帮助将不胜感激。谢谢。
要获得平滑的运动,您需要创建一个线程来处理运动。
为每个键 (w, a, s, d) 创建一个布尔变量,并在按下键时将其设置为 true,在释放键时将其设置为 false。只是一个简单的关键监听器。 别忘了给jframe加上监听,检查jframe是否有焦点
private class InputListener implements KeyListener{
@Override
public void keyPressed(KeyEvent pressed) {
//arrow keys
if(pressed.getKeyCode() == 37){
arrowLeft = true;
}else if(pressed.getKeyCode() == 38){
arrowUp = true;
}else if(pressed.getKeyCode() == 39){
arrowRight = true;
}else if(pressed.getKeyCode() == 40){
arrowDown = true;
}
//System.out.println(pressed.getKeyCode());
}
@Override
public void keyReleased(KeyEvent released) {
if(released.getKeyCode() == 37){
arrowLeft = false;
}
if(released.getKeyCode() == 38){
arrowUp = false;
}
if(released.getKeyCode() == 39){
arrowRight = false;
}
if(released.getKeyCode() == 40){
arrowDown = false;
}
}
@Override
public void keyTyped(KeyEvent arg0) {}
}
我用它来用箭头键移动,所以对于 wasd 移动你需要改变键码。您需要的第二件事是在按下某个键时移动正方形的后台线程。它根据经过的速度和时间计算运动,因此非常流畅。
long currTime = 0;
long lastTime = 0;
new Thread() {
@Override
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
currTime = System.currentTimeMillis();
long time = currTime - lastTime;
int movePixel = (int) (speed * time);
if (movePixel < 1 ) {
currPlayerTime -= time;
}else{
if(arrowLeft){
//move left by movePixel pixel
}else if(arrowRight){
//move right by movePixel pixel
}
if(arrowUp){
}else if(arrowDown){
}
}
lastTime = currTime;
}
}
}.start();
这可能不是最简单和最快的解决方案,但绝对是最好的:)
Is there a way to prevent to the hesitation of the movement.
您可以使用 Swing Timer 来安排动画。查看 Motion Using the Keyboard 的工作示例。