Search code examples
swiftinterface-builderskscenesknode

Hide node in SKScene in interface builder


I have several SKScene done in interface builder in which I have positioned several sprites within the part of the SKScene that is visible on the screen

I want some sprites to only appear later so I start to hide them at the beginning in the didMoveToView method.

override func didMoveToView(view: SKView)
{
        // Reading of the sprites created in the SKScene in the interface builder
    BackgroundLayer = self.childNodeWithName("BackgroundLayer") as! SKSpriteNode
    LettreB = self.childNodeWithName("B0") as! SKSpriteNode
    LettreB.hidden = true
    LettreR = self.childNodeWithName("R0") as! SKSpriteNode
    LettreR.hidden = true
    LettreI = self.childNodeWithName("I0") as! SKSpriteNode
    LettreI.hidden = true
}

The issue I have is that when running the app I very briefly see all sprites before they are hidden.

Even if I set the blendfactor to 1 (and blendmode to Alpha) in interface builder, the sprites - that are then not visible anymore in the interface builder - very briefly appear then disappear when running the app

Is there a way to avoid that except by putting the sprites outside of the visible area and moving them when I need them to appear ? Should I put the code above somewhere else before didMoveToView is called ?

Note : this issue only happens when running the app directly from the device, not when the app is launch through Xcode, for some performance reasons I guess.


Solution

  • Usually using uppercase to the properties name it's considered as a bad attitude, you should use backgroundLayer or lettreB

    This kind of changes can be do it in different ways (during the SKScene init methods, or by customizing your SKSpriteNode and change his inits), one of them is in the previous SKScene or UIViewController where you create the scene and call it:

     override func viewDidLoad() {
            super.viewDidLoad()
            print("---")
            print("∙ \(NSStringFromClass(self.dynamicType))")
            print("---")
            let skView = self.view as! SKView
            if let scene = MyScene(fileNamed:"MyScene") {
               var letter = scene.childNodeWithName("LettreB") as! SKSpriteNode //for example
               letter.hidden = true //for example
               skView.presentScene(scene)
            }
     }