2D game C# WPF - Player Jump code not allowing player to move in air

A N 81 Reputation points
2020-11-28T02:46:56.893+00:00

Hello. I am trying to make a simple 2D game with a person that can walk and jump over objects.
I made a function - person_Jump(), it is supposed to make the player jump and then fall.
My problem is that when the function is being run I can not move the person left or right,
and by the time the function ends the player is on the ground again. How can I fix this?

Code:

        private async void person_Jump()
        {
            double originalX = personX;
            double originalY = personY;

            for (int i = 0; i < 10; i++) // Run a jump animation.
            {
                personY -= 10; // personY : person.Margin.Top
                await Task.Delay(10);
            }

            for (int i = 0; i < 10; i++) // Run a fall animation.
            {
                personY += 10; // personX : person.Margin.Left
                await Task.Delay(10);
            }
        }

EDIT: Ok, I got that part working fine.
Now I need the player to keep momentum when jumping,
and not stop moving.

Request clarification if needed.

Thanks in advance.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,279 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-11-28T13:37:12.81+00:00

    Hi,
    your code works fine in my demo:

    <Window x:Class="WpfApp1.Window016"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp016"  
            mc:Ignorable="d"  
            Title="Window016" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
        <Canvas>  
        <Button Canvas.Top="5" Canvas.Left="5" Content="Left" Command="{Binding}" CommandParameter="Left"/>  
        <Button Canvas.Top="5" Canvas.Left="55" Content="Right" Command="{Binding}" CommandParameter="Right"/>  
        <Button Canvas.Top="5" Canvas.Left="105" Content="Jump" Command="{Binding}" CommandParameter="Jump"/>  
        <Ellipse Height="10" Width="10" Fill="Red" Canvas.Top="{Binding PersonY}" Canvas.Left="{Binding PersonX}"/>  
      </Canvas>  
    </Window>  
    

    And ViewModel:

    using System;  
    using System.ComponentModel;  
    using System.Runtime.CompilerServices;  
    using System.Threading.Tasks;  
    using System.Windows;  
    using System.Windows.Input;  
      
    namespace WpfApp016  
    {  
      public class ViewModel : ICommand, INotifyPropertyChanged  
      {  
        public double PersonX { get; set; } = 10;  
      
        private double _personY = 100;  
        public double PersonY  
        {  
          get => this._personY;  
          set { this._personY = value; OnPropertyChanged(); }  
        }  
      
        public void Execute(object parameter)  
        {  
          switch (parameter.ToString())  
          {  
            case "Left":  
              PersonX -= 10;  
              OnPropertyChanged(nameof(PersonX));  
              break;  
            case "Right":  
              PersonX += 10;  
              OnPropertyChanged(nameof(PersonX));  
              break;  
            case "Jump":  
              person_Jump();  
              break;  
            default:  
              break;  
          }  
        }  
      
        private async void person_Jump()  
        {  
          for (int i = 0; i < 10; i++) // Run a jump animation.  
          {  
            PersonY -= 10;  
            await Task.Delay(100);  
          }  
      
          for (int i = 0; i < 10; i++) // Run a fall animation.  
          {  
            PersonY += 10;  
            await Task.Delay(100);  
          }  
        }  
      
        public event PropertyChangedEventHandler PropertyChanged;  
        private void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));  
      
        public event EventHandler CanExecuteChanged;  
        public bool CanExecute(object parameter) => true;  
      }  
    }  
    

    Result:

    43403-x.gif

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful