Random Duration in SKAction in Swift -


how can random 2 objects’ duration?

  func start() {      let ajusdtedduration = nstimeinterval (frame.size.width / kdefaultxtomovepersecond)      let moveleft = skaction.movebyx(-frame.size.width/2, y: 0, duration: ajusdtedduration/2)     let resetposition = skaction.movetox(0, duration: 0)     let movesequence = skaction.sequence([moveleft, resetposition])      runaction(skaction.repeatactionforever(movesequence)) }  func stop() { removeallactions()   } 

and one. plus, how can make both of duration synced?

  func startmoving() { let moveleft = skaction.movebyx(-kdefaultxtomovepersecond, y: 0, duration: 1)     runaction(skaction.repeatactionforever(moveleft)) } 

you can create random number , use duration.

the function that:

func randombetweennumbers(firstnum: cgfloat, secondnum: cgfloat) -> cgfloat{     return cgfloat(arc4random()) / cgfloat(uint32_max) * abs(firstnum - secondnum) + min(firstnum, secondnum) } 

to sync both actions, don't need much. because if call actions after each other, started @ same time. call function once , use both of functions:

//number between 0 , 10 let randomtime = randombetweennumbers(0, secondnum: 10) 

then use it. best way use parameter in functions:

func startmoving(randomtime: cgfloat) { let moveleft = skaction.movebyx(-kdefaultxtomovepersecond, y: 0, duration: randomtime)     runaction(skaction.repeatactionforever(moveleft)) } 

as see use randomtime parameter duration:

startmoving(randomtime) 

the same in start()-function:

func start(randomtime:cgfloat) 

Comments