5

Sorry if this sounds too primitive. I really don't seem to get how the wait SKAction works for a range. I've seen a few posts, but I they don't clearly explain (to my understanding) how to calculate my range. For example I saw the ranges below:

SKAction.wait(forDuration: 2.5, withRange: 3.0),  //Wait between 1.0 and 4.0 seconds
SKAction.wait(forDuration: 0.65, withRange: 0.7),//Wait between 0.3 and 1.0 seconds

I am not sure how to reconcile the above to calculate a waiting times of between 1.0 to 2.0 seconds and 0.2 to 0.8 seconds.

IronThrone
  • 242
  • 1
  • 10

1 Answers1

5

The forDuration time is the average time of the action. the withRange times gives a tolerance on either side of the forDuration time.

Divide the withRange time by 2 and add/subtract it from the forDuration time.

SKAction.wait(forDuration: 2.5, withRange: 3.0),  //Wait between 1.0 and 4.0 seconds
//3.0 / 2 = 1.5; 2.5 - 1.5 = 1.0; 2.5 + 1.5 = 4.0

SKAction.wait(forDuration: 0.65, withRange: 0.7),//Wait between 0.3 and 1.0 seconds
//0.7 / 2 = 0.35; 0.65 - 0.35 = 0.3; 0.65 + .035 = 1.0

So if you want wait times between 1.0 and 2.0 seconds use

SKAction.wait(forDuration: 1.5, withRange: 1.0)

and for times between 0.2 and 0.8 seconds, use

SKAction.wait(forDuration: 0.5, withRange: 0.6)

A general formula for calculating forDuration and withRange values is this:

forDuration = t_min + (t_max - t_min) / 2
withRange = (t_max - t_min)
claassenApps
  • 1,137
  • 7
  • 14