UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

2      

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.

2      

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.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.