另一个 KeyListener/KeyBinding 问题 [英] Yet another KeyListener/KeyBinding Issue

查看:22
本文介绍了另一个 KeyListener/KeyBinding 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

呃,我很抱歉 MadProgrammer,但我无法让 KeyBinding 以我想要的方式工作 :(.但我会继续看更多的教程,直到我弄清楚为止.现在虽然我已经坚持使用 KeyListener 并且它可以工作.但是现在我遇到了 p.move(); 并没有真正移动播放器的问题.我放入的所有其他代码都可以正常工作除了 p.move();.我可能不应该问这么多问题,所以如果你想让我停下来就这么说吧,但整个 SO 社区都非常好.再次,我'将发布代码.

Ugh, I'm sorry MadProgrammer but I just couldn't get the KeyBinding to work the way I wanted it to :(. But I'll keep looking at some more tutorials till I figure it out. For now though I've stuck to a KeyListener and it works. But now I'm having an issue where p.move(); doesn't actually move the player. All other code I've put in works fine except p.move();. I probably shouldn't be asking this many questions, so if you want me to stop just say so, but the whole SO community is really nice. Again, I'll post the code.

主类:

import javax.swing.*;

public class Game extends JFrame{
public static void main(String[] args){
    new Game();
}
public Game(){

    add(new Board());

    setTitle("Hi mom");
    setSize(555,330);
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(3);
    setVisible(true);

}
}

董事会类:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel implements ActionListener {

Image background;
Player p;
boolean moving;

public Board() {

    setFocusable(true);
    requestFocus();

    addKeyListener(new KeyInputEvents());

    Timer timer = new Timer(25, this);
    timer.start();

    ImageIcon img = new ImageIcon(getClass().getResource("images/map.png"));
    background = img.getImage();

    p = new Player();

}

public void paint(Graphics g) {
    g.drawImage(background, 0, 0, null);
    g.drawImage(p.getPlayer(), p.getX(), p.getY(), null);
}

public void actionPerformed(ActionEvent e) {
    repaint();
}
public JPanel getBoard(){
    return this;
}
)

玩家等级(这可能是出错的地方):

Player Class (This is probably where stuff goes wrong):

import javax.swing.*;
import java.awt.*;
public class Player{
int x = 30;
int y = 187;
Image player;
public Player(){
    ImageIcon img = new ImageIcon(getClass().getResource("images/player.png"));
    player = img.getImage();
}
public Image getPlayer(){
    return player;
}
public void move(int x, int y){
    this.x += x;
    this.y += y;
}
public int getX(){
    return x;
}
public int getY(){
    return y;
}
)

KeyInputEvents 类:

KeyInputEvents Class:

import java.awt.event.*;

import javax.swing.*;

public class KeyInputEvents extends KeyAdapter implements ActionListener{
int k;
boolean moving = true;
Player p = new Player();
public KeyInputEvents(){
    Timer timer = new Timer(25,this);
    timer.start();
}
public void keyPressed(KeyEvent e){
    k = e.getKeyCode();
    moving = true;
}
public void keyReleased(KeyEvent e){
    moving = false;
}
public void actionPerformed(ActionEvent e) {
    if(k == 'D' && moving == true){p.move(5,0);}
    if(k == 'A' && moving == true){p.move(-5,0);}
}

}

推荐答案

屏幕上的 Player 与您在 Player 不一样>KeyInputEvents 类...

The Player that is on the screen is not the same Player you are moving in your KeyInputEvents class...

Board 中,你创建了一个 Player...

In Board you create an instance of Player...

public class Board extends JPanel implements ActionListener {
    Player p;
    public Board() {
        //...
        p = new Player();
    }

然后在 KeyInputEvents 中创建另一个...

And in KeyInputEvents you create another one...

public class KeyInputEvents extends KeyAdapter implements ActionListener {
    //....
    Player p = new Player();

这两个实例没有任何关系...

These two instances are in no way related...

当我在这里时,您不应该真正覆盖 paint,而是覆盖 paintComponent 并且您绝对应该调用 super.paintXxx

While I'm here, you shouldn't really override paint, but instead, override paintComponent and you should definitely be calling super.paintXxx

使用键绑定示例进行更新

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

public class Game extends JFrame {

    public static void main(String[] args) {
        new Game();
    }

    public Game() {

        add(new Board());

        setTitle("Hi mom");
        setSize(555, 330);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(3);
        setVisible(true);

    }

    public class Board extends JPanel implements ActionListener {

        Image background;
        Player p;
        private int xDelta, yDelta;

        public Board() {

            setFocusable(true);
            requestFocus();

            ImageIcon img = new ImageIcon(getClass().getResource("/images/map.jpg"));
            background = img.getImage();

            p = new Player();

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "Left.move");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "Left.stop");

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "Right.move");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "Right.stop");

            ActionMap am = getActionMap();
            am.put("Left.move", new MoveLeft(this));
            am.put("Left.stop", new StopAllMovement(this));
            am.put("Right.move", new MoveRight(this));
            am.put("Right.stop", new StopAllMovement(this));

            Timer timer = new Timer(25, this);
            timer.start();

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
            g.drawImage(background, 0, 0, null);
            g.drawImage(p.getPlayer(), p.getX(), p.getY(), null);
        }

        protected void setMovement(int xDelta, int yDelta) {
            this.xDelta = xDelta;
            this.yDelta = yDelta;
        }

        public void actionPerformed(ActionEvent e) {
            p.move(xDelta, yDelta);
            repaint();
        }

        public JPanel getBoard() {
            return this;
        }

    }

    public class Player {

        int x = 30;
        int y = 187;
        Image player;

        public Player() {
            ImageIcon img = new ImageIcon(getClass().getResource("/images/player.png"));
            player = img.getImage();
        }

        public Image getPlayer() {
            return player;
        }

        public void move(int x, int y) {
            this.x += x;
            this.y += y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }
    }

    public abstract class AbstractMove extends AbstractAction {

        private Board board;
        private int xDelta;
        private int yDelta;

        public AbstractMove(Board board, int xDelta, int yDelta) {
            this.board = board;
            this.xDelta = xDelta;
            this.yDelta = yDelta;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            board.setMovement(xDelta, yDelta);
        }

    }

    public class MoveLeft extends AbstractMove {

        public MoveLeft(Board board) {
            super(board, -5, 0);
        }

    }

    public class MoveRight extends AbstractMove {

        public MoveRight(Board board) {
            super(board, 5, 0);
        }

    }

    public class StopAllMovement extends AbstractMove {

        public StopAllMovement(Board board) {
            super(board, 0, 0);
        }

    }

}

这篇关于另一个 KeyListener/KeyBinding 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆