简体   繁体   English

KeyListener与键绑定

[英]KeyListener vs. Key Bindings

People here keep suggesting to me to use Key Binding instead of KeyListener in Java 2D games. 这里的人们不断向我建议,在Java 2D游戏中使用Key Binding而不是KeyListener

What are the advantages and disadvantages of each one? 每个人的优点和缺点是什么? Is Key Bindings really better for 2D games? 键绑定真的对2D游戏更好吗?

KeyListener is a much lower level API which requires the component that it is registered to be focused AND have keyboard focus. KeyListener是一个较低级别的API,它要求注册它的组件必须具有焦点并具有键盘焦点。 This can cause issues when you have other components within your game that may grab keyboard focus, for example. 例如,当您的游戏中有其他可能吸引键盘焦点的组件时,这可能会引起问题。

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. 通常, KeyListener很难维护,扩展或更改,因为通常所有按键事件都是通过单个侦听器传递的,因此所有游戏​​控件都源自此单个位置。

(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) (现在想象您要添加其他控件,例如按钮或什至操纵杆或控制器进行混合-您突然需要考虑任意数量的输入集线器,保持最新并同步:P)

The Key Bindings API has been designed to provide re-usable Action s 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... Key Bindings API旨在提供可重复使用的Action ,可在Swing API的各种不同部分中使用,虽然这使桌面应用程序开发更加容易,但在开发游戏时也变得更加容易...

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 现在,您不仅可以控制生成事件的焦点级别,而且还可以灵活地定义可映射到不同操作的可配置键

For example... 例如...

You define an Up Action , which moves you character up. 您定义一个Up Action ,它将使您的角色向上移动。 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. 这意味着,该Action并不关心它是如何触发的,只关心它在触发时应该做什么。

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. 与此相关的最重要的一点是,它突然具有向用户提供自定义功能的能力,因此,他们实际上可以定义所需的按键动作-例如,而不必设计自己的按键映射系统。

It also means that you can use the same Action (and even the same instance) in a variety of different ways. 这也意味着您可以通过各种不同的方式使用同一Action (甚至同一实例)。 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... 例如,您可以将Action绑定到按键,然后将其添加到按钮,如果您足够勇于尝试,甚至可以将其绑定到其他输入设备(如操纵杆或控制器)...但是您需要构建API自己来实现,但这意味着您突然对所有用户输入有了一个API概念,例如...

Key bindings were introduced later. 稍后介绍了键绑定。 They map actions to specific keys whereas a listener simply listens for which keys are pressed. 它们将动作映射到特定的键,而侦听器仅侦听按下的键。 To be honest, it doesn't really matter which one you use, but it's always preferable to use key bindings. 老实说,使用哪一个并不重要,但是使用键绑定始终是更可取的。

There are many libraries also available which have their advantages/disadvantages. 也有许多具有其优点/缺点的库。 Key bindings should be fine though for a 2D game. 尽管对于2D游戏来说,键绑定应该没问题。 However, please note that using the Java API is not recommended for game development. 但是,请注意,不建议在游戏开发中使用Java API。 If you ever want to build 3D, or content rich 2D games, it's much better to use OpenGL. 如果您想构建3D或内容丰富的2D游戏,最好使用OpenGL。 Try LWJGL or JOGL (LWJGL is preferred generally) or you can use a game engine such as Slick2D or LibGDX. 尝试使用LWJGL或JOGL(通常首选LWJGL),也可以使用游戏引擎,例如Slick2D或LibGDX。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM