Java Swing Keylistener 2D Boulderdash [英] java swing keylistener 2d boulderdash

查看:49
本文介绍了Java Swing Keylistener 2D Boulderdash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个难题,但首先我只是想让玩家移动,他被允许移动到不是岩石(1)或墙壁(0)的任何空间.

I'm looking to make a boulderdash problem but first i'm just trying to get the player to move around, he's allowed to move to any space that is not a rock(1) or a wall(0).

我的向上和向左运动正常,但向右和向下运动被搞砸了,即使只按了一次键,它们也移动了多个空格.如果有帮助,请使用以下可视化文件,我发布了所有相关代码,但我猜该错误在按键监听器"部分

My up and left motions work fine but right and down are screwed up, they're moving multiple spaces even if the key is only pressed once. Here's a visualization if it helps, i'm posting all the relevant code but i'm guessing the bug is in he keylistener section

这是我在其他网站上上传的代码 http://textuploader.com/13k1

Here's the code i uploaded on another website http://textuploader.com/13k1

我花了几分钟试图将代码复制粘贴到此处,但是代码功能但并非所有代码都被归类为代码,因此我无法提交

I spent several minutes trying to copy paste the code here but the code function but not all the code was being classified as code and i wasn't allowed to submit it

这是其他相关类和文件的链接

Here are the links to other relevant classes and files

推荐答案

首先永远不要使用数字常量而不是字段常量,

First of all never use the numeric constants instead of the field constants,

public void keyPressed(KeyEvent k) {
    int keyCode = k.getKeyCode();           
    if(keyCode == KeyEvent.VK_R) // not keyCode == 82
}

一种更好的方法是使用InputMap和ActionMap,我认为它们也称为键绑定"(由MadProgrammer建议).输入映射将击键映射到动作名称,而动作映射将动作名称映射到您要执行的动作.

What would be a better approach is to use an InputMap and ActionMap, which I think is also called "Key Binding" (as suggested by MadProgrammer). The input map maps a key stroke to an action name, and the action map maps the action name to the action you want to take.

替换您的行(以及整个KeyListener扩展类)

Replace your line (and the whole KeyListener extended class)

this.addKeyListener(new MyKeyListener());

类似

this.getInputMap().put(KeyStroke.getKeyStroke("control L"), "link");

您需要参考 KeyStroke.getKeyStroke 的文档,以根据需要修改指定的按键.在我的示例中,链接"是按下CTRL + L时要执行的操作的名称.现在,我们需要指定链接的作用"

where you need to refer to the documentation of KeyStroke.getKeyStroke to modify the specified key stroke to your needs. In my example "link" is the name of the action to be taken when CTRL+L is pressed. Now we need to specify what "link does"

this.getActionMap().put("link", new LinkAction());

其中LinkAction是我的类,扩展了 AbstractAction ,在您的情况下,该类应包括您的方法,例如 levelReaderObject.setCurrentLevel(presentLevel); .

where LinkAction is my class extending AbstractAction which in your case should includes your methods such as levelReaderObject.setCurrentLevel(presentLevel);.

请注意,您不必为每个键都创建一个动作.对于运动(上,下,左,右),我将所有运动按钮绑定到不同的动作名称(上移"等),但是然后将所有动作名称映射到同一动作,并让该动作中的方法做这项工作:

Note that you don't need to create an Action for every key. For movement (up, down, left, right) I would bind all of the movement buttons to different action names ("move up" etc.), but then map all the action names to the same action and let the methods inside that action do the work:

this.getActionMap().put("move up", new MoveAction(0));
this.getActionMap().put("move down", new MoveAction(1));
this.getActionMap().put("move right", new MoveAction(2));
this.getActionMap().put("move left", new MoveAction(3));

使用

class MoveAction extends AbstractAction {

    int direction;

    public MoveAction (int direction) {

        this.direction = direction;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        switch(direction) // perform the action according to the direction
    }
}

请注意,我建议将运动动作分组在一起是一个设计决定,您应该自己决定如何构造绑定(您可以对所有动作使用一个动作,或者对每个动作使用一个动作).

Please note that my suggestion to grouping the movement actions together is a design decision and you should decide how to structure the bindings yourself (you could use one action for everything or one action for each).

这篇关于Java Swing Keylistener 2D Boulderdash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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