Bram Roelandts
Bram Roelandts

Reputation: 470

Limit sides of gameScene

I am developing a small iOS game in swift. I have got an image randomly appearing on the screen when you tap it. At the bottom I have an iAd banner. When generating random place,arc4random sometimes generates a place where the image is half off-screen or on the iAd. I would like to prevent it from doing that. How could I do that? Any help is appreciated, really! Thanks!

Upvotes: 0

Views: 24

Answers (1)

Gliderman
Gliderman

Reputation: 1205

For the first part of your question, where your sprite is half off the screen, you need to just take the width of the sprite and offset the arc4random by half. Some pseudocode like this (it may work in Swift):

let width = self.sprite.size.width
// Set the position from the random
let x = (arc4random() % (self.viewWidth - width)) + (width / 2)
self.sprite.position = CGPointMake(x, <whatever y value>)

For the second part of your question, you do something similar, but with the height of the sprite and the y position.

Upvotes: 1

Related Questions