NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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 Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.