Имя moveUp не существует в текущем контексте - PullRequest
0 голосов
/ 09 апреля 2020

Я пытаюсь добавить вертикальное движение в учебник по стрельбе сверху вниз, где персонаж игрока в настоящее время перемещается только влево и вправо. Я не понимаю, почему «moveUp» не распознается, когда «moveLeft» и «moveRight», тем более что «if (e.Key == Key.Up)» не выводит никаких ошибок. Заранее спасибо.

private void onKeyDown(object sender, KeyEventArgs e)
        {


            if (e.Key == Key.Left)
            {
                moveLeft = true;
            }
            if (e.Key == Key.Right)
            {
                moveRight = true;
            }
            if (e.Key == Key.Up)
            {
                moveUp = true; //The name 'moveUp' does not exist in the current context
            }
        }
 private void onKeyUp(object sender, KeyEventArgs e)
        {


            if (e.Key == Key.Left)
            {
                moveLeft = false;
            }
            if (e.Key == Key.Right)
            {
                moveRight = false;
            }
            if (e.Key == Key.Up)
            {
                moveUp = false; //The name 'moveUp' does not exist in the current context
            }

public partial class MainWindow : Window
    {   //make a game timer
        DispatcherTimer gameTimer = new DispatcherTimer();
        // move left and move right boolean declaration
        bool moveLeft, moveRight, moveUp; //moveUp added by me. 'The value it is assigned to is never read'

 private void gameEngine(object sender, EventArgs e)
        {
            // link the player hit box to the player rectangle
            playerHitBox = new Rect(Canvas.GetLeft(player), Canvas.GetTop(player), player.Width, player.Height);

            // reduce one from the enemy counter integer
            enemyCounter--;

            scoreText.Content = "Score: " + score; // link the score text to score integer
            damageText.Content = "Damaged " + damage; // link the damage text to damage integer

            // if enemy counter is less than 0
            if (enemyCounter < 0)
            {
                makeEnemies(); // run the make enemies function
                enemyCounter = limit; //reset the enemy counter to the limit integer
            }

            // player movement begins

            if (moveLeft && Canvas.GetLeft(player) > 0)
            {
                // if move left is true AND player is inside the boundary then move player to the left
                Canvas.SetLeft(player, Canvas.GetLeft(player) - playerSpeed);
            }
            if (moveRight && Canvas.GetLeft(player) + 90 < Application.Current.MainWindow.Width)
            {
                // if move right is true AND player left + 90 is less than the width of the form
                // then move the player to the right
                Canvas.SetLeft(player, Canvas.GetLeft(player) + playerSpeed);




Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...