sceneDidLoad Running Twice

拟墨画扇 提交于 2020-02-08 05:10:51

问题


When I run my program. The code I put into "override func sceneDidLoad()" runs two times.

E.g.

Note: I have no idea why this picture is not uploading, but it shows "spawn" happening twice.

This code should only run once when "sceneDidLoad()" is called.

Here is the code for the "sceneDidLoad" function, and for the "testSpawn()" function (which is the specific one that gave the duplicated printout).

class GameScene: SKScene {

    var mapTerrain: SKTileMapNode!


    override func sceneDidLoad() {
        cam = SKCameraNode()
        cam.xScale = 1
        cam.yScale = 1
        //do zoom by change in scale in pinch. (E.g. if they start out 5 units apart and end up 15 units apart, zoom by a factor of 3
        self.camera = cam
        self.addChild(cam)
        cam.position = CGPoint(x: 100, y: 100)
        setupLayers()
        loadSceneNodes()
        setUpUI()
        testSpawn()
        //print("\(self.frame.width), \(self.frame.height)")
    }
     func testSpawn(){
        let RedLegion = legion(texture: textureRedLegion, moveTo: nil, tag: 1, health: 2)
        RedLegion.position = mapTerrain.centerOfTile(atColumn: 0, row: 0)
        RedLegion.team = "Red"
        unitsLayer.addChild(RedLegion)
        legionList.append(RedLegion)
        print("spawn")
    }
}

Note: Not all of the code is here (like "setUpLayers()"), if needed I can supply it, I just do not think it is neccessary.


回答1:


Search your whole document for "print("spawn")" just to make sure that is the only time you call the function. Also check for "testSpawn()" to make sure it is only called once. Additionally, instead of relying on this print to count how many times the sceneDidLoad runs, place a print directly within your sceneDidLoad. Finally, check to make sure you are not creating the scene twice.




回答2:


I've also seen this and submitted a bug report but apple responded saying that it is intended behavior. Apple said that it creates a dummy scene and then creates the actual scene. Before it runs the second time it gets rid of anything done the first time so you shouldn't get any errors from it. The bug is really hard to reproduce, one of my friends working off the same repository that I was but did not experience the bug.




回答3:


I changed sceneDidLoad to didMoveToView:(SKView *)view if you are looking for a solution to this. Make sure you xcode is up to date.



来源:https://stackoverflow.com/questions/42680004/scenedidload-running-twice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!