Skip to content

Commit 8ebd519

Browse files
committed
removed win and refactored animation code
1 parent 90d5844 commit 8ebd519

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

samples/Templates/2DPlatformer/_2DPlatformer/_2DPlatformer.Game/PlayerController.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Windows.Input;
88
using Stride.Graphics;
99
using Stride.Rendering.Sprites;
10+
using System.Collections.Generic;
1011

1112
namespace _2DPlatformer.Player;
1213

@@ -21,6 +22,7 @@ public class PlayerController : SyncScript
2122
private float animationTimer = 0f;
2223
// Animation Spped: Every 1/10 passed seconds the next frame will be played. Closer to 1.0 is a slower Animation.
2324
private const float animationInterval = 1f / 10f;
25+
private int idleFrame = 0;
2426
private int runFrame = 0;
2527
private int jumpFrame = 0;
2628

@@ -82,7 +84,7 @@ private void HandleInput()
8284
}
8385

8486
// Up (Jump)
85-
if (Input.IsKeyDown(Keys.W) && characterComponent.IsGrounded || Input.IsKeyDown(Keys.Up) && characterComponent.IsGrounded)
87+
if (Input.IsKeyPressed(Keys.W) && characterComponent.IsGrounded || Input.IsKeyPressed(Keys.Up) && characterComponent.IsGrounded)
8688
{
8789
characterComponent.Jump();
8890
}
@@ -116,27 +118,30 @@ private void HandleAnimation(bool isMoving)
116118
/// </summary>
117119
private void PlayIdleAnimation()
118120
{
119-
jumpFrame = 0;
120-
121121
if (animationTimer >= animationInterval)
122122
{
123123
animationTimer -= animationInterval;
124-
spriteComponent.CurrentFrame = (spriteComponent.CurrentFrame + 1) % IDLE_FRAME_END;
124+
spriteComponent.CurrentFrame = idleFrame;
125+
idleFrame = (idleFrame + 1) % IDLE_FRAME_END;
126+
127+
//spriteComponent.CurrentFrame = (spriteComponent.CurrentFrame + 1) % IDLE_FRAME_END;
128+
125129
}
130+
ResetFrameCounts(ref runFrame, ref jumpFrame);
126131
}
127132

128133
/// <summary>
129134
/// When there is movement to either side, play run-animation. Which loops frames 4-19
130135
/// </summary>
131136
private void PlayRunAnimation()
132137
{
133-
jumpFrame = 0;
134138
if (animationTimer >= animationInterval)
135139
{
136140
animationTimer -= animationInterval;
137-
runFrame = (runFrame + 1) % RUN_FRAME_END;
138141
spriteComponent.CurrentFrame = IDLE_FRAME_END + runFrame;
142+
runFrame = (runFrame + 1) % RUN_FRAME_END;
139143
}
144+
ResetFrameCounts(ref idleFrame, ref jumpFrame);
140145
}
141146

142147
/// <summary>
@@ -154,5 +159,16 @@ private void PlayJumpAnimation()
154159
jumpFrame++;
155160
}
156161
}
162+
ResetFrameCounts(ref idleFrame, ref runFrame);
163+
}
164+
165+
/// <summary>
166+
/// Resets frame counters to 0.
167+
/// Example: When playing the run animation, this is called to reset the idle and jump animation counter, to ensure they start at their first frame.
168+
/// </summary>
169+
private void ResetFrameCounts(ref int counter, ref int counter2)
170+
{
171+
counter = 0;
172+
counter2 = 0;
157173
}
158174
}

samples/Templates/2DPlatformer/_2DPlatformer/_2DPlatformer.Game/_2DPlatformer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net10.0-windows</TargetFrameworks>
4+
<TargetFrameworks>net10.0</TargetFrameworks>
55
</PropertyGroup>
66

77
<ItemGroup>

0 commit comments

Comments
 (0)