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 根本沒有顯示在模擬器上。所以我想知道是否有任何解決方案和任何愿意回答的人。感謝您的時間。還有,這個代碼來自主角的類,類的名字是“玩家”
uj5u.com熱心網友回復:
您let soarAnimation 在createAnimations 函式中宣告,這意味著當您呼叫self.run(soarAnimation). 解決方案:宣告soarAnimation為類屬性。替代解決方案:createAnimations()回傳 SKAction 并以這種方式在 init 中獲取它
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/386667.html
上一篇:未呼叫WKWebView導航委托
