Search code examples
swiftsprite-kitskspritenodeskview

SKSpriteNodes in SKView


I am trying to get all the SKSpriteNodes in my SKView to change their alpha. Here's my code:

if let nodes = self.gameSKView.scene!.children as? [SKSpriteNode] {
        for node in nodes {
            if node.name != "bg" {
                node.alpha = 0
            }
        }
}

With those codes, the nodes is always nil but when I print what in my gameSKView it is not nil.

println("\(self.gameSKView.scene!.children)")

Can anyone try to help me? Thanks in advance.


Solution

  • self.gameSKView.scene!.children as? [SKSpriteNode]
    

    is non-null if all the children are SKSpriteNode - as? is not a filter to select elements of a particular type, it's a type-safe "cast".

    I's very difficult to provide any advice how to do what you want without knowing what you want to accomplish and more of the code.