如何创建过渡到 SKAction?

编程入门 行业动态 更新时间:2024-10-19 13:21:08
本文介绍了如何创建过渡到 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?

本文发布于:2023-10-24 14:25:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524172.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SKAction

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!