vinny_711
vinny_711

Reputation: 51

Applying an impulse to an SKSprite in a random direction

I have a ball that I would like to apply an impulse to. This I know how to do. What I don't know how to do is randomize the direction of the ball. How would this be done.

Upvotes: 0

Views: 177

Answers (1)

WangYudong
WangYudong

Reputation: 4423

The way to generate random float:

func randomFloatBetween(lower: CGFloat, upper: CGFloat) -> CGFloat {
    return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * (upper - lower) + lower
}

Use it to make your random CGVector.

Upvotes: 1

Related Questions