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:)
或者更具体地说:
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/