矢印キーでイメージ(宇宙船の画像)を制御してUP/DOWN/R/Lに移動する宇宙船ゲームを作成しようとしています。 このために私はCoreWindow.KeyDownイベントを使用します。KeyDownイベントでのキャンバス上でのスムース要素モーションUWP
実際にはうまく動作しますが、動きが十分に滑らかではありません。 矢印キーの1つを押すたびに画像が表示されます。 1. 1ステップをその方向に移動します。 2.フリーズします(半分以下の時間)。 3.次に問題なく移動します。 (「ステップ」は、ピクセル数(たとえば、20)を保持する「int」変数です)。
もちろん、これはゲームを実行する方法ではありません。 「半分の秒」の遅れがないのに矢印キーの1つを押すと、船がすぐにスムーズに動くようにしたい。 これは私のコードです:ところで
public sealed partial class MainPage : Page
{
double playerYAxisPosition;
double playerXAxisPosition;
int steps = 20;
bool upMovement;
bool downMovement;
bool rightMovement;
bool leftMovement;
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
Window.Current.CoreWindow.KeyUp += CoreWindow_KeyUp;
}
// Recognizes the KeyDown press and sets the relevant booleans to "true"
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args) {
playerYPosition = (double) playerShip.GetValue(Canvas.TopProperty);
playerXPosition = (double) playerShip.GetValue(Canvas.LeftProperty);
if (args.VirtualKey == Windows.System.VirtualKey.Up) {
upMovement = true;
}
else if (args.VirtualKey == Windows.System.VirtualKey.Down) {
downMovement = true;
}
else if (args.VirtualKey == Windows.System.VirtualKey.Left) {
leftMovement = true;
}
else if (args.VirtualKey == Windows.System.VirtualKey.Right) {
rightMovement = true;
}
movePlayer();
}
// recognizes the KeyUp event and sets the relevant booleans to "false"
private void CoreWindow_KeyUp(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args) {
if (args.VirtualKey == Windows.System.VirtualKey.Up) {
upMovement = false;
}
else if (args.VirtualKey == Windows.System.VirtualKey.Down) {
downMovement = false;
}
else if (args.VirtualKey == Windows.System.VirtualKey.Left) {
leftMovement = false;
}
else if (args.VirtualKey == Windows.System.VirtualKey.Right) {
rightMovement = false;
}
}
// Calls the movement Methods of the relevant direction
private void movePlayer() {
if (upMovement) {
moveUp();
}
if (downMovement) {
moveDown();
}
if (rightMovement) {
moveRight();
}
if (leftMovement) {
moveLeft();
}
}
private void moveUp() {
playerShip.SetValue(Canvas.TopProperty, playerYPosition - stepsToMove);
}
private void moveDown() {
playerShip.SetValue(Canvas.TopProperty, playerYPosition + stepsToMove);
}
private void moveRight() {
playerShip.SetValue(Canvas.LeftProperty, playerXPosition + stepsToMove);
}
private void moveLeft() {
playerShip.SetValue(Canvas.LeftProperty, playerXPosition - stepsToMove);
}
、私はすべてのKeyDownイベントに「true」または「false」に設定されており、直接KeyDownイベントを使わないであろういくつかの専用のブール値を作成した理由は、さその分離によって、要素(船舶の画像)も斜めに移動することができますが、「args.VirtualKey == Windows.System.VirtualKey.SomeArrowKey」を使用すると何らかの理由で直接許可されません。
ありがとうございました。
私はこのためにgoogleを逆さまにしました。 ありがとうございました! 問題が解決しました:) –