我有一个通过按下按钮调用新场景的游戏。当你在一个新的场景中点击发送相应的图片。当您返回游戏 map 时,内存总是增加 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)
}
}
}
最佳答案
本质上,可能有很多因素会导致您的游戏内存增加。
我尽力帮助您进行一些有用的更正。
谈到自定义协议(protocol),您可以通过在行尾添加 class
来打破强引用,并向委托(delegate)声明 weak var
:
protocol ResumeBtnSelectorDelegate: class {
func didPressResumeBtn(resumeBtn:SKSpriteNode)
}
weak var resumeBtnDelegate:ResumeBtnSelectorDelegate?
...
Speaking about completion
可能是对 self
的强引用,所以你可以像这个例子那样做:
self.launchVideo(completion: {
[weak self] success in
guard let strongSelf = self else { return }
//self.showMyVideo()
strongSelf.showMyVideo()
}
同样的事情来运行 Action block :
let exp = SKAction.run {
[weak self] in
guard let strongSelf = self else { return }
strongSelf.getExplosion(percentageDimension: 15,type: 0,position: enemy.position)
}
如果您在 objective-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..
}
关于ios - 切换到新场景时内存会增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39819048/