Apple Game Frameworks and Technologies: fadeIn to alpha 1.0 when alpha was already 1.0 (p 143)

On page 143 is this code:

// Add the 'chomp' text at the player's position
let chomp = SKLabelNode(fontNamed: "Nosifer")
chomp.name = "chomp"
chomp.fontSize = 22.0
chomp.text = "gloop"
chomp.horizontalAlignmentMode = .center
chomp.verticalAlignmentMode = .bottom
chomp.position = CGPoint(x: player.position.x, y: player.frame.maxY + 25)
chomp.zRotation = CGFloat.random(in: -0.15...0.15)
addChild(chomp)

// Add actions to fade in, rise up, and fade out
let fadeIn = SKAction.fadeAlpha(to: 1.0, duration: 0.05)
let fadeOut = SKAction.fadeAlpha(to: 0.0, duration: 0.45)
let moveUp = SKAction.moveBy(x: 0.0, y: 45, duration: 0.45)
let groupAction = SKAction.group([fadeOut, moveUp])
let removeFromParent = SKAction.removeFromParent()
let chompAction = SKAction.sequence([fadeIn, groupAction, removeFromParent]) 
chomp.run(chompAction)

The fadeIn action fades alpha to 1.0 over 0.05 seconds. It is the first action in the sequence. But the label node hasn’t had its alpha tweaked and the default is already 1.0. So the fadeIn action, as it is currently written, is just adding a small delay but no fade.

Question:

Was it intended for this to actually fade? If so then we should add chomp.alpha = 0 to the upper code block. If not then perhaps the fadeAlpha action should just be a wait action?

Admittedly nobody will ever notice the difference either way due to the very short duration of 0.05 seconds. But I feel like the code doesn’t match the intent right now. I’m just not sure what the intent was.

2 Likes

Hi, David.

Thanks for the message.

To answer your question, the code you see in the book does indeed match the intent behind it.

For this label, I wanted a very subtle fade in effect to help prevent a jarring visual response from the player. The 0.05 seconds works to achieve this.

Of course, if you’d rather skip the fadeIn action, increase its duration, or use a wait action instead, that works too.

It’s always fun to see what others come up with on their own. There was a reader on Twitter who shared his progress with the game, and he added some extra code that randomly changes the text. I thought it was well done and added some extra charm to our blobby little green friend. :grin:

Thanks again for your comments. I appreciate all of the feedback you’re giving about this book.

EDIT: My apologies, I looked a little closer at the code, and you’re right—I am missing the initial setting of chomp.alpha = 0.0. You’ll see this correction in the next update.

1 Like