Swift Game Development, Part 2, fading a SpriteNode that doesn’t want to fade

Today I learned about the “isPaused” instance property of an SKNode object.   The problem I encountered was that after showing some informational text, such as when your player sprite “talks” to a non-player character (NPC), you want to fade the text out.  The code works fine on iOS, but the MacOS program refused to fade the text.

When you want to programmatically create a sequence of actions for a sprite node to execute, Swift and the SpriteKit provide a plethora of ways to accomplish this.   The easiest way is to create a few SKActions and then chain them together with sequences and finally run them:

        let wait:SKAction = SKAction.wait(forDuration: waitTime)

        let fade:SKAction = SKAction.fadeAlpha(to: 0, duration: 0.5)

        let run:SKAction = SKAction.run {

            self.infoLabel1.text = ""

            self.infoLabel2.text = ""

            self.infoLabel1.alpha = 1

            self.infoLabel2.alpha = 1

            self.speechIcon.alpha = 1

            self.speechIcon.isHidden = true

        }

What I discovered was that the run action was not executing.   After much reading, I found that the isPaused property on both the labels and the parent of the labels was “true”.   Very strange.   I did not find out why the nodes were paused, but when I added a check on the parent to see if it was paused and then toggled it if it was, the “run” sequence was able to execute:

        let seq:SKAction = SKAction.sequence([wait, fade, run])

        let seq2:SKAction = SKAction.sequence([wait, fade])

        if(infoLabel2.parent?.isPaused)! {

            infoLabel2.parent?.isPaused = false   //the fix!

        }

        infoLabel1.run(seq)

        infoLabel2.run(seq2)

        speechIcon.run(seq2)

Thank you Stack Overflow, you’re the best pair programmer ever!        

       

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s