KeyListener的与键绑定 [英] KeyListener vs. Key Bindings

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

问题描述

这里的人们不断暗示我在Java 2D游戏使用的键绑定,而不是的KeyListener

People here keep suggesting to me to use Key Binding instead of KeyListener in Java 2D games.

什么是每个人的优点和缺点?是按键绑定真的2D游戏好?

What are the advantages and disadvantages of each one? Is Key Bindings really better for 2D games?

推荐答案

的KeyListener 是一个低得多的API,它要求它注册到有重点,有分量键盘焦点。这可能会导致问题,当你有你游戏中的其他组件,可能会抢键盘焦点,例如。

KeyListener is a much lower level API which requires the component that it is registered to be focused AND have keyboard focus. This can cause issues when you have other components within your game that may grab keyboard focus, for example.

的KeyListener 一般比较难以维护和扩展或改变,因为通常情况下,所有的关键事件通过一个侦听渠道,因此,所有的游戏控制从这个起源单一的立场。

KeyListener is generally more difficult to maintain and extend or change, as typically, all the key events are channelled through a single listener, so ALL the game controls originate from this single position.

(现在假设你想添加其他控件,如按钮,甚至操纵杆或控制器混合 - 你突然有你需要考虑,不断更新和同步任意数量的输入枢纽:P)

(Now imagine you want to add other controls, such as buttons or even joystick or controllers to mix - you suddenly have any number of input hubs you need to consider, keep up to date and in sync :P)

键绑定API的设计是为了提供可重复使用的<​​code>动作■哪些可以在各种摇摆API的不同部分的使用,而这使得桌面应用程序开发更加容易,它也可以更容易开发游戏的时候...

The Key Bindings API has been designed to provide re-usable Actions which can be used in a variety of different parts of the Swing API, while this makes desktop application development easier, it can also make it easier when developing games...

除了事实,你现在过的聚焦程度增益控制所产生的事件,您也可以灵活界定配置关键的它可以映射到不同的动作

Apart from the fact that you now gain control over the focus level that the events are generated, you also gain the flexibility to defining configurable key's which can be mapped to different actions

例如...

您定义一个最多 动作,其中移动你的性格了。这是从任何情况下离婚。这意味着,在动作不关心它是如何引发的,只有它被触发时,它应该做的。

You define an Up Action, which moves you character up. This is divorced from any event. That means, the Action does not care how it is triggered, only what it should do when it is triggered.

您现在可以自由定义这将触发此操作的击键。有关此的很大一部分,是突然必须提供定制到用户的能力,所以它们实际上可以定义他们想要的操作的关键笔划 - 例如,无需设计某种自己的密钥映射系统的

You are now free to define the keystroke which would trigger this action. The great part about this, is suddenly have the ability to provide customisation to the user, so they can actually define the key stroke they want for the action - for example, without having to design some kind of key mapping system of your own.

这也意味着,你可以在各种不同的方式使用同一个动作(甚至相同的实例)。例如,您可以将绑定在动作来击键,并将其添加到按钮,如果你足够勇敢去尝试,甚至将其绑定其他输入设备(如游戏杆或控制器)...但你需要建立自己的API来实现它,但它意味着你突然有你的所有用户输入一个单一的API的概念,例如...

It also means that you can use the same Action (and even the same instance) in a variety of different ways. For example, you can bind the Action to a key stroke and add it to button and if you're brave enough to try, even bind it another input device (like a joystick or controller)...but you'd need to build the API yourself to achieve it, but it means you suddenly have a single API concept for all your user input, for example...

这篇关于KeyListener的与键绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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