[英]Swift Sprite-kit: How do you set an animation for a random time?
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)
I want to make the animation to be set at a random time interval (random time between 1 second and 3 seconds). 我想将动画设置为随机时间间隔(1秒至3秒之间的随机时间)。 Then, if the screen is touched when the red light is up, I want a game over sign to appear.
然后,如果在红灯亮时触摸了屏幕,我希望出现游戏结束标志。 Thank You in advance.
先感谢您。
What you need to use is SKAction.waitForDuration(_:withRange:) 您需要使用的是SKAction.waitForDuration(_:withRange :)
https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKAction_Ref/index.html#//apple_ref/occ/clm/SKAction/waitForDuration:withRange : https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKAction_Ref/index.html#//apple_ref/occ/clm/SKAction/waitForDuration:withRange :
Or more specifically: 或更具体地说:
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)
According to the documentation, your animation will last 2 seconds +/- 1 second. 根据文档,您的动画将持续2秒+/- 1秒。
Also note that I took the liberty of changing your "var" variables to "let" constants, because they are not being changed. 另请注意,我可以自由地将您的“ var”变量更改为“ let”常量,因为它们没有被更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.