Bram Roelandts
Bram Roelandts

Reputation: 470

Random place in frame

I am developing a small game but I'm stuck on this. I want to generate a random position for an SKSpriteNode called div. I have this code

    let random = arc4random() % UInt32(self.frame.size.width)
    let random1 = arc4random() % UInt32(self.frame.size.height)

    div.position = CGPoint(x: Int(random), y: Int(random1))

    self.addChild(div)

I can't figure out what's wrong and why the div is sometimes out of the frame and not visible. Does anyone has a good solution for this? Any help is appreciated! Thanks!

Upvotes: 1

Views: 55

Answers (1)

Sami
Sami

Reputation: 587

try setting this in your didMoveToView,

self.size = view.bounds.size

Upvotes: 1

Related Questions