question

AN-5534 avatar image
0 Votes"
AN-5534 asked ZsomborSzke-6929 commented

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

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.



dotnet-csharpwindows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PeterFleischer-3316 avatar image
1 Vote"
PeterFleischer-3316 answered ZsomborSzke-6929 commented

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



x.gif (243.8 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ok, I actually got it to work after a bit.

The only problem now is that I run and try to jump and my movement stops before
jumping so I have to press the key to walk again while in air.

0 Votes 0 ·

Hey, sorry I know this post is old, but I don't understand how your binding works. I used the code you wrote, except I didn't use buttons, I used the KeyDown event, and then I send the key that was pressed to a controller class, that evaluates what key was pressed, and calls the viewmodel's method which depending on the input decreases or increases the playerX coordinate. But unfortunately, it doesn't seem to work. Any leads?

0 Votes 0 ·