切换到新场景时内存增加

时间:2016-10-02 16:20:36

标签: ios swift sprite-kit

我有按下按钮调用新场景的游戏。当您在新场景中单击时发送相应的图片。当您返回游戏地图时,内存总是增加30 MB。我不明白最强的链接在哪里。仪器无法检测泄漏。对不起我的英文。请帮帮我。

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch: AnyObject in touches {

        let location = touch.location(in: self)

        for i in 0...3 {


            if  childNode(withName: "button\(i)")!.isHidden == false && childNode(withName: "button\(i)")!.contains(location)  {
                buttonOfBattlefield = childNode(withName: "button\(i)")
                }
            }


                switch buttonOfBattlefield?.name {
                case "button0"?:
                    battlefieldName = "A"
                case "button1"?:
                    battlefieldName = "B"
                case "button2"?:
                    battlefieldName = "C"
                case "button3"?:
                    battlefieldName = "D"

                default:
                    break
                }

            if battlefieldName != nil {
                let myScene = GameScene(size: self.size , battlefield: battlefieldName!)
                myScene.scaleMode = self.scaleMode
                let reveal = SKTransition.fade(withDuration: 2.0)
                self.view?.presentScene(myScene, transition: reveal)


        }

    }
}

2 个答案:

答案 0 :(得分:1)

基本上可能会有很多因素导致游戏内存增加。

我尽力帮助你做一些有用的修正。

谈到自定义协议,您可以通过在行末添加class来破解强引用,并向代理声明weak var

protocol ResumeBtnSelectorDelegate: class {
    func didPressResumeBtn(resumeBtn:SKSpriteNode)
}

weak var resumeBtnDelegate:ResumeBtnSelectorDelegate?
...

说到completion可能是对self的强烈引用,所以你可以这样做:

self.launchVideo(completion: {
   [weak self] success in
   guard let strongSelf = self else { return }
   //self.showMyVideo()
   strongSelf.showMyVideo()
}

运行动作块同样的事情:

let exp = SKAction.run {
          [weak self] in
          guard let strongSelf = self else { return }
          strongSelf.getExplosion(percentageDimension: 15,type: 0,position: enemy.position)
}

如果在目标C中使用第三方库,则可能还需要删除强引用:

__weak __typeof__(self) weakSelf = self;
SKAction *sequence = [SKAction sequence:@[[SKAction followPath:_ascentPath duration:1.5], [SKAction runBlock:^(void){[weakSelf explode];}]]];
[self runAction:sequence];
}

如果你有一些观察者尝试删除它,NSTimers的同样的东西到willMoveFromView方法。

override func willMove(from view: SKView) {
   //remove UIKit objects, observers, timers..
}

答案 1 :(得分:0)

找到解决方案。新的游戏场景需要使用该方法。

self.enumerateChildNodes(withName: "//*"){ (node, stop) -> Void in
        node.removeAllActions()
        node.removeAllChildren()
        node.removeFromParent()
    }

谢谢大家的帮助。我很高兴!