我的部分动画在 SpriteKit 中不起作用
Posted
技术标签:
【中文标题】我的部分动画在 SpriteKit 中不起作用【英文标题】:Part of my animation is not working in SpriteKit 【发布时间】:2022-01-21 01:36:25 【问题描述】:init ()
super.init(texture: nil, color: .clear, size: initialSize)
// Create physicsBody based on a frame of the sprite
// basically giving it gravity and physics components
let bodyTexture = textureAtlas.textureNamed("MainCharacterFlying1")
self.physicsBody = SKPhysicsBody(texture: bodyTexture, size: self.size)
// Lose momentum quickly with high linear dampening
self.physicsBody?.linearDamping = 0.9
// weighs around 30 kg
self.physicsBody?.mass = 30
// no rotation
self.physicsBody?.allowsRotation = false
createAnimations()
self.run(soarAnimation, withKey: "soarAnimation")
// Animations to make the main character seem like it is flying
func createAnimations()
let rotateUpAction = SKAction.rotate(byAngle: 0, duration: 0.475)
rotateUpAction.timingMode = .easeOut
let rotateDownAction = SKAction.rotate(byAngle: -1, duration: 0.8)
rotateDownAction.timingMode = .easeIn
let flyFrames: [SKTexture] = [textureAtlas.textureNamed("MainCharacterFlying1"), textureAtlas.textureNamed("MainCharacterFlying2"),textureAtlas.textureNamed("MainCharacterFlying3"), textureAtlas.textureNamed("MainCharacterFlying4"),textureAtlas.textureNamed("MainCharacterFlying5"),textureAtlas.textureNamed("MainCharacterFlying4"),textureAtlas.textureNamed("MainCharacterFlying3"),textureAtlas.textureNamed("MainCharacterFlying2")]
var flyAction = SKAction.animate(with: flyFrames, timePerFrame: 0.1)
flyAction = SKAction.repeatForever(flyAction)
flyAnimation = SKAction.group([flyAction,rotateUpAction])
let soarFrames: [SKTexture] = [textureAtlas.textureNamed("MainCharacterFlying5")]
var soarAction = SKAction.animate(with: soarFrames, timePerFrame: 1)
soarAction = SKAction.repeatForever(soarAction)
let soarAnimation = SKAction.group([soarAction,rotateDownAction])
当我在 ios 模拟器上运行此代码时,我必须在屏幕上单击一次才能让我的主精灵出现在屏幕上,否则它不会出现。当我点击精灵时,精灵将开始拍打翅膀并上升(我有其他代码)但是,rotateUpAction 和 rotateDownAction 根本没有出现在模拟器上。所以我想知道是否有任何解决方案以及愿意回答的人。 感谢您的时间。 另外,这段代码来自主角的类,类名是“玩家”
【问题讨论】:
【参考方案1】:你声明let soarAnimation
在你的createAnimations函数,这意味着当你调用self.run(soarAnimation)
时它不在作用域内。解决方案:将soarAnimation
声明为类属性。替代解决方案:让createAnimations()
返回 SKAction 并以这种方式在 init 中获取它
【讨论】:
以上是关于我的部分动画在 SpriteKit 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章