ios - Swift Sprite 套件 : How do you set an animation for a random time?

标签 ios animation swift sprite-kit skaction

    var lightTexture = SKTexture(imageNamed: "green light.png")
    var lightTexture2 = SKTexture(imageNamed: "red light.png")

    var animationLight = SKAction.animateWithTextures([lightTexture, lightTexture2], timePerFrame:     3)
    var changeLight = SKAction.repeatActionForever(animationLight)

    light = SKSpriteNode(texture: lightTexture)
    light.position = CGPointMake(CGRectGetMidX(self.frame), 650)
    light.runAction(changeLight)

    self.addChild(light)

我想将动画设置为随机时间间隔(1 秒到 3 秒之间的随机时间)。然后,如果在红灯亮起时触摸屏幕,我希望出现一个游戏结束标志。提前谢谢你。

最佳答案

你需要用到的是SKAction.waitForDuration(_:withRange:)

https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKAction_Ref/index.html#//apple_ref/occ/clm/SKAction/waitForDuration:withRange :

或者更具体地说:

let lightTexture = SKTexture(imageNamed: "green light.png")
let lightTexture2 = SKTexture(imageNamed: "red light.png")

let animateLights = SKAction.sequence([
    SKAction.waitForDuration(2.0, withRange: 2.0),
    SKAction.animateWithTextures([lightTexture, lightTexture2], timePerFrame: 3)
    ])

let changeLight = SKAction.repeatActionForever(animateLights)

let light = SKSpriteNode(texture: lightTexture)
light.position = CGPointMake(400, 650)
light.runAction(changeLight)

self.addChild(light)

根据文档,您的动画将持续 2 秒 +/- 1 秒。

另请注意,我冒昧地将您的“var”变量更改为“let”常量,因为它们没有被更改。

关于ios - Swift Sprite 套件 : How do you set an animation for a random time?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27873931/

相关文章:

ios - Facebook 分享对话框深度链接 iOS

用于坠落、振动等的 CSS 动画

animation - 减慢引导微调器

swift - 在结构内指定 Hashable 类型约束

iOS 日期时区转换不起作用

ios - 如何在每次切换文本时执行不同的 Action ?

ios - iOS 中是否有类似 Android onPreviewFrame 的方法

ios - 在 objective-c 中从 firebase 检索数据

iphone - "Masking"动画? iPhone SDK

ios - 如何在循环内对 UIView 进行动画 n 次并跟踪每个动画事件