简体   繁体   English

如何逐步(动画)更改Spritekit中的背景颜色?

[英]How to gradually (animated) change background color in Spritekit?

SOLUTIONS I TRIED: 解决方案我试过:

Gradually change background color in Swift 在Swift中逐渐改变背景颜色

Swift : Background Color fading animation (SpriteKit) Swift:背景颜色褪色动画(SpriteKit)


WHAT I CURRENTLY HAVE (not working): 我目前有什么(不工作):

class GameScene: SKScene, SKPhysicsContactDelegate{

    var background = SKSpriteNode()

    var colorizeToRed = SKAction()


    override func didMoveToView(view: SKView) {

        background = SKSpriteNode(color: UIColor.blueColor(), size: self.frame.size)

        background.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)

        background.zPosition = 15

        colorizeToRed = SKAction.colorizeWithColor(SKColor(red: 90, green: 0, blue: 0, alpha: 1), colorBlendFactor: 1.0, duration: 2.0)

        background.runAction(colorizeToRed)

    }

}

MY PROBLEM: 我的问题:

I don't see the background change color. 我没有看到背景改变颜色。 The only time I see it change color is if I set it directly with no animation using self.backgroundColor = UIColor().aColor . 我唯一一次看到它改变颜色是我使用self.backgroundColor = UIColor().aColor直接设置它没有动画。

EDIT: In fact, I dont'even see the blue background... Something is wrong with my SKSpriteNode, but what ? 编辑:事实上,我也不会看到蓝色背景......我的SKSpriteNode有问题,但是什么?


QUESTION: 题:

How to gradually (animated) change background color in Spritekit ? 如何逐步(动画)更改Spritekit中的背景颜色?


BEFORE YOU ASK: 在你问之前:

1) YES, I have already tried changing the zPosition in case the SKNode was hidden. 1)是的,我已经尝试在隐藏SKNode的情况下更改zPosition。

2) YES, I have checked the doc for SKAction.colorizeWithColor() . 2)是的,我已经检查了SKAction.colorizeWithColor()的文档。

3) YES, I am almost certain the solution to my problem is very simple (probably some mistake on my part), but what ? 3)是的,我几乎可以肯定我的问题的解决方案非常简单(可能是我的一些错误),但是什么?

4) YES, I am open to any suggestions telling me the error is somewhere else in my code, but only if you precise where that error might be. 4)是的,我愿意接受任何建议,告诉我错误是在我的代码中的其他地方,但只有在您确定错误的位置时才会这样。

5) NO, I will not post my ENTIRE code here, please ask for specific parts if necessary. 5)不,我不会在这里发布我的整个代码,如有必要,请询问具体部分。


Thank you in advance for any help you provide ^^ ! 提前感谢您提供的任何帮助^^!

By the looks of it, I think you just forgot to add the background to the scene. 从它的外观来看,我认为你只是忘了将背景添加到场景中。 Try this: 尝试这个:

addChild(background)

If that doesn't help, read on... 如果这没有帮助,请继续阅读......

If you're just fading in an SKSpriteNode then you can use SKAction.fadeIn to fade the node in. Just set its alpha to 0 to start and fade it in by doing something like this: 如果您只是淡入SKSpriteNode,那么您可以使用SKAction.fadeIn淡化节点。只需将其alpha设置为0即可启动并通过执行以下操作淡入它:

background.run(SKAction.fadeInWithDuration(2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM