NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

SOLVED: SpriteKit node alpha property

Forums > Swift

Is there a way to bind a property (alpha for instance) of one node to that of it's parent (or inversely, have the parent impost it's alpha property on the child)?

I have a player node with a child SKEmitter. on player.run(SKNode.fadeIn(duration: 1.0), the child emitter node does not change. I'd like the child to match (or better yet, be slightly lower than) the parent...but for the fade and SKActions to translate down to the child.

I'd prefer to not have to run a SKAction(...) on every child of the parent if I can avoid it. A more dynamic soloution would be great.

Here is how I'm setting up my player/child:

        let sparkRibbon: SKEmitterNode = SKEmitterNode(fileNamed: "SparkRibbon")!
        sparkRibbon.xScale = 0.25
        sparkRibbon.yScale = 0.3
        sparkRibbon.zPosition = self.zPosition - 0.1
        self.addChild(sparkRibbon)

Thanks...

   

SpriteKit will automatically perform what you are looking for. If a parent node's alpha changes, so too will all of its children's alphas. From what I can tell here (give the prescence of self), it seems as if you are adding the emitter node to the scene and not to the player node. Changing

self.addChild(sparkRibbon)

to

yourPlayerNode.addChild(sparkRibbon)

and running the action on yourPlayerNode should resolve the issue. If this doesn't help or self refers to yourPlayerNode, the other possbility is that the action is cancelled before it can be effectively run. Check to make sure that removeAllActions() or removeAction(forKey:) is not called by the SKNode instance representing your player node. Since you mentioned that the player node fades but the child doesn't, the first solution should do the trick.

   

Thanks @TwinHeadedDragon for the explanation. FYI: The self in this case is the instance of the PlayerNode object which is an extension of SKSpriteNode...so effectively the player instance knows how to fade itself as appropriate.

   

Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.