这是我的代码。我假设这是因为deinit从未被调用,并且每次用户开始播放(GameScene)时,内存从20 MB跳到40 MB到60等等,并且每次用户死亡并重新开始游戏后FPS都会下降。
有谁知道为什么?
GameViewController.swift
import UIKit
import SpriteKit
extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file as String, ofType: "sks") {
var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! GameScene
archiver.finishDecoding()
return scene
} else {
return nil
}
}
}
class GameViewController: UIViewController{
@IBOutlet var skSceneReal: SKView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
if((self.skSceneReal) != nil)
{
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
var skView = self.view as! SKView
skSceneReal.showsFPS = false
skSceneReal.showsNodeCount = false
/* Sprite Kit applies additional optimizations to improve rendering performance */
skSceneReal.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
scene.viewController = self
skSceneReal.presentScene(scene)
}
}
}
deinit
{
println("deinit worked")
}
override func viewWillDisappear(animated: Bool) {
self.skSceneReal.removeFromSuperview()
self.skSceneReal = nil;
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
GameScene.swift
...
var gameEnding = false;
func gameOver(){
if(!gameEnding) {
gameEnding = true;
// Deallocate/reset the view/scene.
// ???? DONT KNOW WHAT TO DO ????
// Present move segue.(Created in storyboard) Source: GameVC, Destination: GameOverVC
self.viewController?.performSegueWithIdentifier("move", sender: self)
}
}
通过GameOverViewController中的按钮(在Storyboard中创建),可以回到GameViewController模式。
答案 0 :(得分:1)
我遇到了同样的问题。我通过将场景中的所有节点归零来修复它,而不是显示UIViewController而是显示另一个SKScene。这似乎解决了这个问题并正确地解除了GameScene的负担。
这是我的方法游戏:
- (void) gameOver {
[self removeAllActions];
[self removeAllChildren];
SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
SKScene *newScene;
newScene = [[GameOverScene alloc] initWithSize:self.size andNewHighscore:score];
[self.view presentScene:newScene transition: reveal];
}