使用箭头键而不是拇指摇杆移动玩家

发布于 2024-12-05 04:52:06 字数 267 浏览 0 评论 0原文

我正在开发一款 2d 游戏,我希望玩家能够使用箭头键移动。

我已经设法让玩家通过左拇指杆(Xbox 360 控制器)移动,

GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
hero.position.X += gamePadState.ThumbSticks.Left.X * 20;

但我不知道如何用箭头键移动它。

有谁可以帮助我吗? :)

I'm working on a 2d game and I want the player to be able to be moved with the arrow keys.

I've managed to make the player move by the left thumb stick (Xbox 360 controller)

GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
hero.position.X += gamePadState.ThumbSticks.Left.X * 20;

But I can't figure out how to move it with a arrow keys.

Anyone thing the could help me? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2024-12-12 04:52:06

要处理键盘,您可以使用 Keyboard.GetState() 返回KeyboardState。获得键盘状态后,您可以调用 KeyboardState.IsKeyDown() 来确定您感兴趣的键是否已被按下。如果按下该键,则相应地调整 x 和 y 坐标。

请记住,拇指操纵杆是模拟输入,因此您可以获得一系列值,指示操纵杆向左、向右、向上或向下移动的距离,这允许玩家对“速度”进行一定的控制。键盘是数字的,因此要么按下,要么不按下,因此角色要么移动,要么不移动。有一些技巧可以让键盘上的感觉相似,但首先让你的角色移动,然后如果你需要的话,你可以深入研究它。

您还应该看看 XNA 附带的平台入门工具包,该代码应该为您提供一些关于如何处理输入设备的想法。

To handle the keyboard, you can use the Keyboard.GetState() which returns KeyboardState. Once you have the keyboard state you can call KeyboardState.IsKeyDown() to determine if the key you are interested in has been pressed. If the key is pressed, then you adjust the x and y coordinates accordingly.

Remember the thumb sticks are analogue inputs so you can get a range of values indicating how far left, right, up or down the stick has been moved, which allows the player some control over the "speed". The keyboard is digital so it is either pressed, or not so the character is either moving or not. There are tricks to make this feel similar on the keyboard, but first get your character moving, then you can delve into that if you even require it.

You should also take a look at the Platform starter kit that comes with XNA, that code should give you a few ideas around how to handle input devices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文