Swift:SKLabelNode淡化动画不会褪色

时间:2022-04-18 10:56:42

I'm just trying to make a SKLabelNode fade in, here's my code:

我只是想让SKLabelNode淡入,这是我的代码:

let welcome = SKLabelNode(text: "Welcome")
welcome.fontName = "HelveticaNeue-Light"
welcome.fontSize *= size.width/welcome.frame.width
welcome.fontColor = UIColor(white:1,alpha:0)
welcome.horizontalAlignmentMode = .center
welcome.verticalAlignmentMode = .center
welcome.position = CGPoint(x:size.width/2,y:size.height/2)
addChild(welcome)

let fadein = SKAction.fadeIn(withDuration: 1)
let remove = SKAction.removeFromParent()
welcome.run(SKAction.sequence([fadein,remove]))


But it doesn't work, and I can't figure out why.
The strange part is the removeFromParent part works fine, just not the fade in.
I already tried changing the font, making the label fade out and even making a custom action that changes the alpha, all of which have failed.
I just can't figure out what the problem is.

但它不起作用,我无法弄清楚原因。奇怪的部分是removeFromParent部分工作正常,只是没有淡入。我已经尝试更改字体,使标签淡出,甚至做一个自定义动作,改变alpha,所有这些都失败了。我只是无法弄清楚问题是什么。

Any idea will be appreciated.
Thanks in advance.

任何想法将不胜感激。提前致谢。

1 个解决方案

#1


2  

Instead of setting the fontColor's alpha to 0, set the SKLabelNode's alpha to 0 before running the fadeIn action on it. This is because the actions are applied to the nodes themselves, not to property inside of the nodes. (E.G. In your case: fadeIn affects SKLabel.alpha, not SKLabel.fontColor.alpha)

不要将fontColor的alpha设置为0,而是在对其运行fadeIn操作之前将SKLabelNode的alpha设置为0。这是因为操作应用于节点本身,而不是应用于节点内的属性。 (E.G.在你的情况下:fadeIn影响SKLabel.alpha,而不是SKLabel.fontColor.alpha)

#1


2  

Instead of setting the fontColor's alpha to 0, set the SKLabelNode's alpha to 0 before running the fadeIn action on it. This is because the actions are applied to the nodes themselves, not to property inside of the nodes. (E.G. In your case: fadeIn affects SKLabel.alpha, not SKLabel.fontColor.alpha)

不要将fontColor的alpha设置为0,而是在对其运行fadeIn操作之前将SKLabelNode的alpha设置为0。这是因为操作应用于节点本身,而不是应用于节点内的属性。 (E.G.在你的情况下:fadeIn影响SKLabel.alpha,而不是SKLabel.fontColor.alpha)