sprite-kit – 将背景颜色淡入另一种颜色的替代方法

sprite-kit – 将背景颜色淡入另一种颜色的替代方法,第1张

概述在我接触开始的方法我把这个简单的代码行淡化为背景颜色为红色. runAction(SKAction.colorizeWithColor(SKColor.redColor(), colorBlendFactor: 1.0, duration: 1.0)) 一切都工作正常,但问题是代码没有使用ios 7做任何事情.我想知道是否有另一种方法使背景淡入不同的颜色,或者如果有这个代码的ios 7版本. 有多 在我接触开始的方法我把这个简单的代码行淡化为背景颜色为红色.

runAction(SKAction.colorizeWithcolor(SKcolor.redcolor(),colorBlendFactor: 1.0,duration: 1.0))

一切都工作正常,但问题是代码没有使用ios 7做任何事情.我想知道是否有另一种方法使背景淡入不同的颜色,或者如果有这个代码的ios 7版本.

解决方法 有多种方法可以从一种颜色过渡到另一种颜色.最直接的方法之一是在两种颜色之间进行线性插值,方法是将起始颜色的RGB分量逐渐增大一部分,随着时间的推移逐渐减小结束颜色的RBG分量:

red = starting_red * (1.0 - fraction) + ending_red * fractiongreen = starting_green * (1.0 - fraction) + ending_green* fractionblue = starting_blue * (1.0 - fraction) + ending_blue * fraction

分数从0开始,以1为增量结束于1

fraction += delta_time * step_size

实现此方法的一种方法是将代码添加到GameScene的dIDMovetoVIEw方法中.但是,如果您的游戏包含多个场景,则更好的策略是扩展SKAction以添加创建自定义动作的类方法,以便所有场景都可以使用它.

首先,定义一个结构来存储起始和结束RGB颜色分量.在GameScene的定义之外添加它.

struct colorComponents {    var red:CGfloat    var green:CGfloat    var blue:CGfloat    init(color:SKcolor) {        self.init()        var Alpha:CGfloat = 0        color.getRed(&red,green: &green,blue: &blue,Alpha: &Alpha)    }    init() {        red = 0        green = 0        blue = 0    }}

然后,通过添加以下方法来扩展SKAction,将背景颜色更改为另一种颜色.请注意,扩展必须在类之外定义.

extension SKAction {    static func changecolor(startcolor:SKcolor,endcolor:SKcolor,duration:NSTimeInterval) -> SKAction {        // Extract and store starting and ending colors' RGB components        let start = colorComponents(color: startcolor)        let end = colorComponents(color: endcolor)        // Compute the step size        let stepSize = CGfloat(1/duration)        // define a custom class to gradually change a scene's background color        let change = SKAction.customActionWithDuration(duration) {            node,time in            let fraction = time * stepSize            let red = start.red * (1.0 - fraction) + end.red * fraction            let green = start.green * (1.0 - fraction) + end.green * fraction            let blue = start.blue * (1.0 - fraction) + end.blue * fraction            if let scene = node as? SKScene {                scene.backgroundcolor = SKcolor(red: red,green: green,blue: blue,Alpha: 1.0)            }        }        return change    }}

最后,创建并运行SKAction

runAction(SKAction.changecolor(backgroundcolor,endcolor: SKcolor.bluecolor(),duration: 5))

将此添加到SKScene子类中的dIDMovetoVIEw,例如GameScene.

总结

以上是内存溢出为你收集整理的sprite-kit – 将背景颜色淡入另一种颜色的替代方法全部内容,希望文章能够帮你解决sprite-kit – 将背景颜色淡入另一种颜色的替代方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1018435.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存