2017-09-23 92 views
2

我遇到了一个很大的问题,我的应用程序。当我转换到新的场景时,我的MainMenu场景不会释放。SKScene没有定义

//GameViewController 

override func viewDidLoad() { 
    super.viewDidLoad() 

    if let view = self.view as! SKView? { 
     // Load the SKScene from 'GameScene.sks' 
     if let scene = MainMenu(fileNamed: "MainMenu") { 
      scene.scaleMode = .aspectFill 
      view.presentScene(scene) 
     } 
} 

enter image description here

我轻点按钮,进入一个新的SKScene其具有下面的代码:

let transition = SKTransition.doorsOpenHorizontal(withDuration: 1.0) 
let next_scene = LuckyScene(fileNamed: "LuckyScene") 
next_scene?.scaleMode = scaleMode 
view?.presentScene(next_scene!, transition: transition) 

它openes我的幸运场景,但它不调用从主要的DEINIT功能菜单。

之后,如果我做同样的事情,从幸运的场景,比如我想离开现场,回到主菜单,它得到释放留下我一个巨大的问题现场。

let transition = SKTransition.doorsOpenHorizontal(withDuration: 1.0) 
let next_scene = MainMenu(fileNamed: "MainMenu") 
next_scene?.scaleMode = scaleMode 
view?.presentScene(next_scene!, transition: transition) 

如果用户输入Lucky Scene并离开它,则会创建一个新的MainMenu场景。

enter image description here

为什么我的MainMenu的场景是没有得到我的时候过渡到一个新的释放?

+0

你有一个引用你的MainMenu的地方...找到那个属性并使其'weak' – Fluidity

+0

https://developer.apple.com/库/内容/文档/斯威夫特/概念/ Swift_Programming_Language/AutomaticReferenceCounting.html – Fluidity

回答

0

经过很长时间,找到了答案。问题是这个SKAction没有被删除。

let bear_animation : SKAction = SKAction.repeatForever(SKAction.sequence([SKAction.run(idle_animation), SKAction.wait(forDuration: 2.0), SKAction.run(wave_animation), SKAction.wait(forDuration: 3.0)]))   
run(bear_animation, withKey: "bear_animation") 

func idle_animation() 
{ 
    left_arm.run(idle_left_arm)  
    right_arm.run(idle_right_arm) 
    body.run(idle_body) 
} 

func wave_animation() 
{ 
    right_arm.run(wave_right_arm_1) 
    right_arm.run(wave_right_arm_2) 
    left_arm.run(wave_left_arm) 
    body.run(wave_body) 
    left_pupil.run(wave_pupil) 
    right_pupil.run(wave_pupil) 
    left_eyebrow.run(wave_eyebrow) 
    right_eyebrow.run(wave_eyebrow) 
} 

所以,当我提出一个新的场景时,我添加了这样的代码。

removeAction(forKey: "bear_animation") 

let transition = SKTransition.doorsOpenHorizontal(withDuration: 1.0) 
let next_scene = LuckyScene(fileNamed: "LuckyScene") 
next_scene?.scaleMode = scaleMode 
view?.presentScene(next_scene!, transition: transition) 
0

你的问题不是动画动作,而是动作内部的动作。

SKAction.run(idle_animation)是强引用self,让你的精灵持有到idle_animation,并idle_animation是坚持以精灵,这意味着你的保留计数将永远不会为0。我会尽量避免使用功能,而使用弱自我封闭

var idle_animation = 
{ 
    [weak self] in 
    guard let strongSelf = self else return 
    strongSelf.left_arm.run(strongSelf.idle_left_arm)  
    strongSelf.right_arm.run(strongSelf.idle_right_arm) 
    strongSelf.body.run(strongSelf.idle_body) 
} 

这样,一旦精灵已经从场景中移除,因为没有保留将其保持不动,这样动画就会掉落。 (注意,你需要为你的其他动画做这个方法)

+0

我还有一些问题,我可以得到你的微博? –