如何创建过渡到 SKAction? [英] How to create transition into SKAction?

查看:46
本文介绍了如何创建过渡到 SKAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了每 10 秒更改一次背景颜色的函数我想在改变背景颜色时添加过渡.

i created function that change my background color every 10 seconds and i want to add transition when its change the color of the background.

游戏场景:

let wait = SKAction.waitForDuration(10)

        let block = SKAction.runBlock({
            [unowned self] in
            self.backgroundColor = UIColor.randomColor()
            })


        let sequence = SKAction.sequence([wait,block])

        runAction(SKAction.repeatActionForever(sequence), withKey: "colorizing")

感谢您的帮助!

推荐答案

你可以这样做:

override func didMoveToView(view: SKView) {

   colorize()
}


func colorize(){

     let colorize = SKAction.sequence([

          SKAction.colorizeWithColor(UIColor.randomColor(), colorBlendFactor: 1, duration: 3),

          SKAction.runBlock({[unowned self] in self.colorize()})
      ])

     runAction(colorize, withKey: "colorizing")
}

这是一个递归函数,每次 colorizeWithColor 操作完成时都会调用自己.这是必需的,因为只是重复这一点:

This is recursive function which calls itself every time colorizeWithColor action is finished. This is required due to fact that just repeating this:

 SKAction.colorizeWithColor(UIColor.randomColor(), colorBlendFactor: 1, duration: 3)

在动作序列中将背景着色为相同的颜色.之所以会发生这种情况,是因为当您创建一个动作时,您无法随时间更改它(例如,您可以更改其速度或暂停它,但您不能更改 duration 或任何其他传递的参数).相反,我们每次重新创建与某个键相关联的动作.这是来自关于与键关联的操作的文档:

inside an action sequence will colorize the background always to the same color. That will happen because when you create an action once, you can't change it over time (you can change its speed or pause it for example, but you can't change a duration or any other passed parameter).Instead, we re-create the action associated with a certain key each time. And this is from the docs about actions associated with keys:

如果使用相同键的操作已在运行,则将其删除在添加新操作之前.

If an action using the same key is already running, it is removed before the new action is added.

因此,每次我们运行一个与着色"键相关联的新操作时,前一个操作都会被删除,并且始终只有一个操作与该键相关联.

So each time we run a new action, associated with "colorizing" key, the previous action is removed and there will be always only one action with that key.

这篇关于如何创建过渡到 SKAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆