0

所以,我刚刚添加了 InputHandler,这样我就可以使用 wasd(上、下、左、右)移动我的屏幕。但是当我尝试它时,我的 spritesheet 被撕裂了,屏幕不会向上或向下移动,只是对角线(左右工作正常),我不知道为什么会这样。我没有任何错误...

package ca.linus.game;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;


public class InputHandler implements KeyListener {

    public InputHandler(Game game) {
        game.addKeyListener(this);
    }

    public class Key {
        private int numTimesPressed = 0;
        private boolean pressed = false;

        public int getNumTimesPressed() {
            return numTimesPressed;
        }

        public boolean isPressed() {
            return pressed;
        }
        public void toggle(boolean isPressed) {
            pressed = isPressed;
            if (isPressed) numTimesPressed++;
        }
    }

    public List<Key> keys = new ArrayList<Key>();

    public Key up = new Key();
    public Key down = new Key();
    public Key left = new Key();
    public Key right = new Key();


    public void keyPressed(KeyEvent e) {
        toggleKey(e.getKeyCode(), true);
    }

    public void keyReleased(KeyEvent e) {
        toggleKey(e.getKeyCode(), false);
    }

    public void keyTyped(KeyEvent e) {

    }

    public void toggleKey(int keyCode, boolean isPressed) {
        if (keyCode == KeyEvent.VK_W) {up.toggle(isPressed);}
        if (keyCode == KeyEvent.VK_S) {down.toggle(isPressed);}
        if (keyCode == KeyEvent.VK_D) {right.toggle(isPressed);}
        if (keyCode == KeyEvent.VK_A) {left.toggle(isPressed);}

    }
}

这是我的 InputHandler 文件。

所有帮助表示赞赏!

4

0 回答 0