sceneDidLoad being called twice?

2019-02-01 10:27发布

问题:

Using Xcode 8, swift 3 and I create a iOS application using the game template with entities enabled. I notice I was seeing double node count for some initial sprites even though I only used addChild once.

I added

override func sceneDidLoad() {
print(#function) ... } 

to the code and no idea why this is being called twice.

log file...

2016-09-20 10:21:31.482 MMDecon1[3295:791435] SKUtil.m: MGGetBoolAnswer is not available in the simulator.
sceneDidLoad()
sceneDidLoad()

I added

override func didMove(to view: SKView) {..} 

and put my initialisation code in here as a temporary fix.

Does any one know why sceneDidLoad() is being fired twice with the default game app code using entities?

回答1:

Normally, sceneDidLoad is only called one time. However, if a memory warning is sent then UIViewController releases its scene and sets it to nil if the view controller isn't visible. The next time that the scene appears the view controller will reload the scene and call sceneDidLoad again.

You have to assume that sceneDidLoad can be called multiple times.

Implement didReceiveMemoryWarning and log or set a breakpoint to see what is happening.