0

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.

Apple Ramos
  • 265
  • 3
  • 13

1 Answers1

0
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.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • I casted it with "as? [SKSpriteNode]" because I know that all the children in my gameSKView is SKSpriteNode. I want to change the alpha of each node in my gameSKView. Now i want is the proper way because apparently, nodes variable is null – Apple Ramos Nov 11 '14 at 06:40