77using System . Windows . Input ;
88using Stride . Graphics ;
99using Stride . Rendering . Sprites ;
10+ using System . Collections . Generic ;
1011
1112namespace _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}
0 commit comments