如何在UWP中注册两名玩家的活动? [英] How do I register events from two players in UWP?

查看:63
本文介绍了如何在UWP中注册两名玩家的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在用UWP制作游戏。我尝试从一个键盘上安装两个同名玩家(比如一个使用箭头键,另一个使用ASWD)。



在UWP中似乎没有任何KeyPress事件,但我尝试过KeyDown。问题是,只要按下一个键,就会忘记上一个键。因此,两个玩家竞争相同的keydown事件。例如,一次只能有一个人移动他们的玩家对象。



任何想法让这个工作?此外,对于对角线移动(例如向上和向右箭头键)一次有两个键会很好。



谢谢!



Petter



我的尝试:



我已经尝试了KeyDown事件,但它只识别最后一个按键(如在Word处理器中)。

解决方案

您的推测无效。没有什么是被遗忘的,原因很简单:你是那个记住键盘事件数据的人。正确处理的基本架构范例称为调度。没错,你可以处理 KeyDown ,有时可能需要 KeyUp ,你永远不需要 KeyPress 除非您需要处理文本输入过程,比如过滤它。



你需要一个处理行的图层然后键盘事件和分析。它位于处理事件的控件和诸如玩家之类的虚拟游戏元素之间。播放器应该具有一些语义感知事件,例如逻辑左,右,左上等。播放器类可以使用自己的事件参数类型定义自己的事件实例,上述值可以是方向矢量或甚至枚举类型。它还应该提供一种接受事件的机制,它在内部调用那些次要的语义事件。中间层找出哪个玩家实例应该调用哪个次要事件。它还解决了对角线键的问题。这很容易:它应该从相关的一组键中跟踪当前按下的键(这就是为什么你还需要 KeyDown 。然后,例如,事件检测到按下Up取决于按键的状态Left和Right。根据这些状态,Up被解释为辅助事件Up,LeftUp或RightUp。



所有这些东西非常简单,但你需要彻底设计它。



还有一个考虑因素:在事件处理中,事件的排队是典型的。但它们不适合你的case:由于游戏细节,应立即将事件发送到玩家实例(或其他地方)并立即处理。



-SA

Hi all,

I'm creating a game in UWP. I try to have two simulatenous players from one keyboard (say one uses Arrow keys and the other one ASWD).

In UWP there doesn't seem to be any KeyPress event, but I have tried with KeyDown. The problem is that as soon as a key has been pressed, the previous key becomes forgotten. Thus, the two players compete for the same keydown events. For example, only one can move their player object at a time.

Any ideas to to make this work? Also, it would be nice to have two keys at once for diagonal moves (up and right arrow keys for example).

Thanks!

Petter

What I have tried:

I've tried the KeyDown event, but it only recognizes the last key pressed (as in a Word processor).

解决方案

Your speculations are invalid. Nothing is forgotten, by one simple reason: you are the one who make the keyboard event data "remembered". The basic architectural paradigm of proper processing is called "dispatching". That's right, you can handle KeyDown and may sometimes need KeyUp, and you never would need KeyPress unless you need to handle the process of text input, say, filter it.

You need to have a layer which handles "row" keyboard event and analyses then. It sits between the control handling the event and "virtual" game elements such as players. The player should be capable of some "semantic-aware" events, such as logical "left", "right", "left-up", and so on. The player class can define its own event instance with its own event argument type, and the above values can be, say, direction vector or even enumeration type. It also should provide a mechanism of accepting the events, which internally invokes those secondary, "semantic" events. The intermediate layer finds out which player instance should invoke which secondary event. It also solves the problem if diagonal keys. This is easy enough: it should keep track of presently pressed keys from the relevant set of keys (and that's why you also need KeyDown. Then, for example, the event detecting that Up is pressed depends on the state of the keys Left and Right. Depending on those states, Up is interpreted as secondary event Up, LeftUp or RightUp.

All this stuff is pretty simple, but you need to design it thoroughly.

One more consideration: in event handling, the queues of events are typical. But they are not suitable in your case: the events should be delivered to the player instances (or elsewhere) immediately and get handled immediately, due to the gaming specifics.

—SA


这篇关于如何在UWP中注册两名玩家的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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