在模态转换后释放SKScene

时间:2014-07-07 11:14:19

标签: ios xcode swift sprite-kit

我一直使用swift在sprite工具包中创建游戏并遇到了问题。我有两个视图控制器,每个控制器有一个场景,一个模态转换到另一个场景。这一切都在第一次完美运行,但是当我回到第一个视图控制器然后再次转到第二个时,我已经使用了两倍的内存。这给我的印象是没有任何东西被解除分配,但每次我转换到场景时都会重新分配对象。我在乐器中运行了应用程序并得到了相同的结果。在下面的图像中,我从一个场景移动到下一个场景,然后再次回到第一个场景,但它似乎重新分配第一个场景但尚未清除任何内存。由于dealloc方法现在尚未使用,我不知道如何解决这个问题。我将代码发布到下面的第一个视图控制器,以便您查看它。非常感谢。

Instument's recordings

import UIKit
import SpriteKit

class SelectionViewController: UIViewController {

var selectionScene:SelectionScene?
var currentRocketName = ""

@IBOutlet var playButton: UIButton

override func viewDidLoad() {
    super.viewDidLoad()

    if let selectionScene = SelectionScene.unarchiveFromFile("SelectionScene") as? SelectionScene {

        // Configure the view.
        let skView = self.view as SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.multipleTouchEnabled = false

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        selectionScene.scaleMode = .ResizeFill
        selectionScene.viewController = self

        skView.presentScene(selectionScene)

        NSNotificationCenter.defaultCenter().addObserver(selectionScene, selector: "spinnerChanged", name: "spinnerValueChanged", object: nil)
        NSNotificationCenter.defaultCenter().addObserver(selectionScene, selector: "productBought", name: "ProductBought", object: nil);
        NSNotificationCenter.defaultCenter().addObserver(selectionScene, selector: "manageErrorInPurchase", name: "ErrorOccured", object: nil)

    }
}

override func shouldAutorotate() -> Bool {
    return true
}

override func viewDidAppear(animated: Bool)  {
}

@IBAction func playButtonPressed(sender: UIButton) {
    self.performSegueWithIdentifier("moveToGame", sender: nil)
}

override func prefersStatusBarHidden() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return Int(UIInterfaceOrientationMask.AllButUpsideDown.toRaw())
    } else {
        return Int(UIInterfaceOrientationMask.All.toRaw())
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override func viewDidUnload() {
    NSNotificationCenter.defaultCenter().removeObserver(selectionScene)
}

override func viewDidDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(selectionScene)
}

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if segue.identifier == "moveToGame" {
        let destController = segue.destinationViewController as GameViewController
        destController.rocketTexture = SKTexture(imageNamed: self.currentRocketName)
    }
}

}

selectionScenecurrentRocketName一旦加载到视图中就会传递给viewController

2 个答案:

答案 0 :(得分:4)

我还不熟悉Swift,所以我会给你一些Objective-C中的例子。

IBOutlet创建skView。当你要展示另一个ViewController时,从它的superview中删除skView并将其取出:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Need to deallocate GameScene (if game is not paused)
    [self.skView removeFromSuperview];
    self.skView = nil;
    ....
}

当ViewController加载时,不要忘记将skView添加回ViewController的视图:

if (!self.skView.window) {
    [self.view addSubview:self.skView];
}

要轻松检查SKScene是否已取消分配,请将此方法添加到其中:

- (void)dealloc {
    NSLog(@"GAME SCENE DEALLOCATED");
}

答案 1 :(得分:0)

我有类似的问题。

原来我通过将SKScene实例作为另一个类中的委托创建了一个强引用。在声明SKScene类型或UIView类型的每个属性为弱后,我的问题得到了解决:

weak var skScene:SKScene!