Swift游戏开发:动画运行时动态调整Glider节点尺寸方法
Hey there! Let's get that glider's jump animation feeling just right—your idea of scaling up to the jump peak and shrinking back down is perfect for a satisfying, responsive feel.
The Core Issue in Your Code
The problem is sitting right in your SKAction.animate call: you’ve set resize: false. When this flag is false, SpriteKit locks the node to its original size, completely ignoring the actual pixel dimensions of each texture in your array. That’s why even though your PNGs are different sizes, the glider stays static.
Quick Fix: Turn On Resize
The simplest fix is to flip resize to true in both of your animate actions. This tells SpriteKit to adjust the glider node’s size to match each texture as the animation plays:
self.gliderSprite.run(SKAction.sequence([ SKAction.animate(with: gliderTextureArray, timePerFrame: 0.1, resize: true, restore: false), SKAction.wait(forDuration: 0.1), SKAction.animate(with: gliderTextureArray.reversed(), timePerFrame: 0.1, resize: true, restore: false) ]))
Bonus: Smoother, Code-Controlled Scaling
If you want more precise control over the scaling (or don’t want to rely on texture dimensions), you can combine texture animation with a dedicated scaling action. This lets you define exact scale values instead of trusting image sizes:
// Animate textures without resizing the node let textureAnimation = SKAction.animate(with: gliderTextureArray, timePerFrame: 0.1, resize: false, restore: false) // Scale up to 1.5x over the same duration as the texture animation let scaleUp = SKAction.scale(to: 1.5, duration: Double(gliderTextureArray.count) * 0.1) // Run both actions in parallel for the "rise" phase let riseSequence = SKAction.group([textureAnimation, scaleUp]) // Reverse the texture animation and scale back to original size let reverseTextureAnimation = SKAction.animate(with: gliderTextureArray.reversed(), timePerFrame: 0.1, resize: false, restore: false) let scaleDown = SKAction.scale(to: 1.0, duration: Double(gliderTextureArray.count) * 0.1) let fallSequence = SKAction.group([reverseTextureAnimation, scaleDown]) // Put the full jump sequence together self.gliderSprite.run(SKAction.sequence([riseSequence, SKAction.wait(forDuration: 0.1), fallSequence]))
About Your Texture Atlas Location
Great news—storing your images in animation.atlas is exactly the right approach! SpriteKit automatically recognizes folders ending with .atlas as texture atlases, optimizes them for performance, and loads them correctly. You don’t need to move your images anywhere else.
Quick Pre-Flight Checks
- Double-check your
gliderTextureArrayorder: make sure it starts with the smallest glider texture and progresses to the largest (for the jump peak). Reversing it will then shrink back down properly. - Verify your PNGs actually have different pixel sizes—sometimes export settings can accidentally force all images to the same dimensions!
内容的提问来源于stack exchange,提问作者demogorgon

