gpt4 book ai didi

ios - SKScene 每次重启后变慢

转载 作者:行者123 更新时间:2023-11-29 01:50:36 24 4
gpt4 key购买 nike

这是我的代码。我认为这是因为 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 中的按钮返回到 GameViewController 的模式转场(在 Storyboard 中创建)。

最佳答案

我遇到了同样的问题。我通过清空场景中的所有节点来修复它,而不是显示 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];
}

关于ios - SKScene 每次重启后变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31467869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com