嘿,我尝试在 UWP(c#) 中构建游戏,但我无法让我的玩家移动
Hey i try to build a game in UWP(c#) and i cant make my player move
我在网上搜索了一下,只找到了关于如何让对象移动的 WPF 教程,它与 UWP 不同。
e.VirtualKey == Windows.System.VirtualKey.Up)
尝试将此与 if 语句一起使用来更改对象的位置,但无法弄清楚键 work.do 你有什么想法吗?谢谢
所以你可以使用CoreWindow.KeyDown事件来处理键:
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
switch (args.VirtualKey)
{
case Windows.System.VirtualKey.Left:
//Go left
break;
case Windows.System.VirtualKey.Right:
//Go right
break;
case Windows.System.VirtualKey.Up:
//Go up
break;
case Windows.System.VirtualKey.Down:
//Go down
break;
}
//In case you want to check for Control + Key:
var ctrl = Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.Control);
if (ctrl.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))
{
//Control was pressed
if (args.VirtualKey == Windows.System.VirtualKey.W)
{
//Do sprint
}
}
}
我在网上搜索了一下,只找到了关于如何让对象移动的 WPF 教程,它与 UWP 不同。
e.VirtualKey == Windows.System.VirtualKey.Up)
尝试将此与 if 语句一起使用来更改对象的位置,但无法弄清楚键 work.do 你有什么想法吗?谢谢
所以你可以使用CoreWindow.KeyDown事件来处理键:
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
switch (args.VirtualKey)
{
case Windows.System.VirtualKey.Left:
//Go left
break;
case Windows.System.VirtualKey.Right:
//Go right
break;
case Windows.System.VirtualKey.Up:
//Go up
break;
case Windows.System.VirtualKey.Down:
//Go down
break;
}
//In case you want to check for Control + Key:
var ctrl = Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.Control);
if (ctrl.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))
{
//Control was pressed
if (args.VirtualKey == Windows.System.VirtualKey.W)
{
//Do sprint
}
}
}