【问题标题】:Having trouble using KeyEvent使用 KeyEvent 时遇到问题
【发布时间】:2014-06-11 12:57:35
【问题描述】:

我的问题是,我怎样才能让我的 move() 方法使用 KeyEventsKeyEvent.VK_DOWN 工作? 我目前正在尝试使用import java.awt.event.KeyEvent;,其中我将使用箭头键而不是数字键盘键在二维网格中移动玩家。我在我的超类User 和类Player extends User 中有我的操作moveUp(); moveRight(); moveDown();moveLeft(); 并包含关键事件方法。当我使用箭头键时,演员根本不会移动,但是当我手动单击网格中的演员并选择一种方法时,它将移动。因此我的移动方法有效,所以我假设我的 KeyEvent 设置已损坏。提供了显示我手动控制方法的图片。

包含移动方法的用户

package info.gridworld.actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

public class User extends Actor {
    private boolean isStopped = false;

    public User()
    {
        setColor(null);
    }

    public void moveUp(){
        moveTo(getLocation().getAdjacentLocation(Location.NORTH));
    }

    public void moveDown(){
        moveTo(getLocation().getAdjacentLocation(Location.SOUTH));
    }

    public void moveLeft(){
        moveTo(getLocation().getAdjacentLocation(Location.WEST));
    }

    public void moveRight(){
        moveTo(getLocation().getAdjacentLocation(Location.EAST));
    }
}  

Player 类包含 KeyEvents

package game.classes;
import info.gridworld.actor.User;
import java.awt.event.KeyEvent;


public class Player extends User{

    public Player(){
    }

    public void keyPressed(KeyEvent e){
        int keys = e.getKeyCode();
        if((keys == KeyEvent.VK_UP)){
            moveUp();
        }
        else if((keys == KeyEvent.VK_DOWN)){
            moveDown();
        }
        else if((keys == KeyEvent.VK_LEFT)){
            moveLeft();
        }
        else if((keys == KeyEvent.VK_RIGHT)){
            moveRight();
        }
    }


}  

主类

package game.classes;
import info.gridworld.grid.*;

public class PlayerRunner{


    private static GameGrid world = new GameGrid();

    public static void main(String[] args)
    {
        Player player = new Player();
        world.add(new Location(0, 0), player);
        world.show();
  }
}  

【问题讨论】:

  • 我不认为它是这样工作的,World 不是收到关键信息吗?
  • 我建议为此使用 keyBinding
  • @MadProgrammer 我会试试那个例子我也没有看到 ActorWorld 类中的 keyPressed 方法
  • @Rod_Algonquin 我也这么认为,但是 OP 使用的是 gridworld API,它的工作方式略有不同......并不是说不可能,但如果我们能找到适用于 API 的东西它将产生更好的结果,如果失败...hack,hack,hack ;)

标签: java keyevent gridworld


【解决方案1】:

你在扩展Actor,跟GUI没有关系,KeyEvents和相关都是swing的东西。您实际上需要将 KeyListener 添加到 JPanel。据我所知,现在你只在 Actor 类中有额外的方法。

该 GUI 实际上不在 AP 测试中,因此没有太多内容,但看起来您可以扩展 info.gridworld.gui.GridPanel。所以重写构造函数为:

public GridPanel(DisplayMap map, ResourceBundle res)
{
    super(map, res);
    addKeyListener(new KeyListener()
    {
        // Put the KeyListener methods here. 
        // (All of them: KeyListener is an interface
    }
}

这有点粗糙,但我认为它应该可以工作。

我假设您将获得对 Actor 的引用,无论如何您都可以使用箭头键移动它,因此您可以调用它的移动方法。

【讨论】:

  • @user3709396 super(map, res);不符合 JPanel 方法中的参数,需要我删除 map 和 res.......` 构造函数 JPanel(DisplayMap, ResourceBundle) 未定义删除参数以匹配 'JPanel()'`
  • 另外我应该把 move() 方法放在哪里?无法从 gridPanel 访问它们并且将它们移动到网格面板不起作用,因为我无权访问位置获取器
  • 你是在扩展 GridPanel 还是 JPanel?
  • 整件事是我不明白你在做什么,你是在修改 GridPanel 中的构造函数,还是说创建一个单独的类来完成所有这些工作?把这个放在玩家类的用户类中?我可以创建所有的 KeyEvents 等等我只是对在哪里添加它以使其正常工作感到困惑?
  • 对不起,但刚刚意识到这是一种糟糕的方式。发布了一个更好的答案...
【解决方案2】:

World 类实际上有一个 keyPressed() 方法。您可以扩展 World 并覆盖此方法。它会给你一个字符串参数而不是 KeyEvent。使用 javax.swing.KeyStroke.getKeyStroke(description).getKeyCode() 找出按下了哪个键。 World类的来源是她:https://github.com/parkr/GridWorld/blobhttps://github.com/parkr/GridWorld/blob/master/framework/info/gridworld/world/World.javaaster/framework/info/gridworld/world/World.java

至于访问您的 Actor 的方法,我会在 World 类中放置一个 Actor 的引用。然后,当您添加要移动到世界的 Actor 时,您可以将其设置在那里。然后,当您处理击键时,您可以访问要操作的 Actor。

【讨论】:

  • 好的,应该可以,但是位置参数的原因是什么?那是演员的位置吗? (getLocation();)?
  • 另外我相信你的一个链接坏了,我无法编辑我之前的评论,这就是我创建这个新评论的原因。
  • 我认为是在 World gui 中选择的 Actor。你不应该因为你正在做的事情而需要它
猜你喜欢
  • 1970-01-01
  • 2012-02-21
  • 2021-11-08
  • 2014-01-08
  • 2011-03-09
  • 2013-01-30
  • 2016-11-13
  • 2016-07-21
相关资源
最近更新 更多