C# Windows Forms平台游戏:移动时跳跃中断及动画资源求助
Hey there! Let's tackle your two questions one by one—first the movement bug, then the character resources.
The problem you're seeing happens because right now, you're probably triggering movement only once when the A/D key is first pressed (via the KeyDown event), instead of continuously checking if the key is held down. When you press W to jump, the game stops listening for the ongoing A/D hold, so movement cuts out until you re-press the key.
Here's how to fix this with a key state tracking system:
Step 1: Track Key Hold States
First, create a dictionary to keep track of whether each key is currently held down. You'll also want a flag to check if the character is on the ground (to prevent infinite jumping):
private Dictionary<Keys, bool> _keyStates = new Dictionary<Keys, bool> { { Keys.A, false }, { Keys.D, false }, { Keys.W, false } }; private bool _isGrounded = true; // Prevents jumping mid-air
Step 2: Update Key States on KeyDown/KeyUp
Modify your existing KeyDown event (and add a KeyUp event) to update the state of each key:
private void Tutorial_KeyDown(object sender, KeyEventArgs e) { if (_keyStates.ContainsKey(e.KeyCode)) { _keyStates[e.KeyCode] = true; } } private void Tutorial_KeyUp(object sender, KeyEventArgs e) { if (_keyStates.ContainsKey(e.KeyCode)) { _keyStates[e.KeyCode] = false; } }
Step 3: Handle Movement/Jump in a Game Loop
Instead of triggering movement directly in KeyDown, handle it in your game's main update loop (like an Update method or a timer Tick event). This way, you'll continuously check if keys are held down every frame:
private void GameUpdate(object sender, EventArgs e) { // Handle left/right movement if (_keyStates[Keys.A]) { // Replace with your actual left movement logic playerPosition.X -= moveSpeed; } if (_keyStates[Keys.D]) { // Replace with your actual right movement logic playerPosition.X += moveSpeed; } // Handle jumping (only if grounded) if (_keyStates[Keys.W] && _isGrounded) { // Replace with your actual jump force logic playerVelocity.Y = -jumpForce; _isGrounded = false; // Mark as airborne after jumping } // Don't forget gravity and collision detection to update _isGrounded playerVelocity.Y += gravity; playerPosition.Y += playerVelocity.Y; // Example ground collision check (adjust to your game's setup) if (playerPosition.Y >= groundYPosition) { playerPosition.Y = groundYPosition; playerVelocity.Y = 0; _isGrounded = true; // Allow jumping again when grounded } }
This works because we're constantly checking the hold state of A/D every frame, so pressing W won't interrupt the movement—both inputs are tracked independently.
Here are some top platforms for finding high-quality, game-ready GIF character animations (all have options for free or paid assets):
- itch.io: A hub for indie developers sharing assets. Search for tags like
platformerorcharacter animation, and filter for GIF format. You'll find tons of free and affordable assets tailored for jump-and-run games. - OpenGameArt: A dedicated free game resource site with open-license assets. Search for "jump run character gif" to find animations you can use commercially (just double-check the license for each asset).
- GameDev Market: For more polished, professional assets. They have curated packs of 2D character animations, including full sets for movement, standing, and jumping in GIF format.
内容的提问来源于stack exchange,提问作者Ace

