Generate random time for subactions inside of repe

2019-03-01 14:00发布

Hi I'm trying to generate random time intervals everytime the action is repeated but it only generates the time interval randomly only when the vied did load.

Here is my code :

 let moveBucketLeft = SKAction.moveByX(400, y: 0, duration: NSTimeInterval(grabRandomTime()))
    let moveBucketRight = SKAction.moveByX(-400, y: 0, duration: NSTimeInterval(grabRandomTime()))
    moveBucketLeftAndRight = SKAction.sequence([moveBucketLeft,moveBucketRight])
    let repeatMovement = SKAction.repeatActionForever(moveBucketLeftAndRight)
    bucket.runAction(repeatMovement)

The method grabRandomTime just generates a random duration between 1 and 5 .

I need all the help I can help. Thank you!

Here's my generate random value method:

func grabRandomTime() ->UInt32{
    var time = arc4random_uniform(6) + 1
    return time
}

And the action runs one time in viewdidload. I think that maybe why it only take a random value one time. I'm not sure how to run the method everytime. I can't process that in my head :/

1条回答
手持菜刀,她持情操
2楼-- · 2019-03-01 14:31

use let wait = SKAction.waitForDuration(sec, **withRange**: dur) instead of duration: NSTimeInterval(grabRandomTime() in your code. SKAction.waitForDuration with withRange parameter will compute random time interval with average=sec and possible range = dur

查看更多
登录 后发表回答