Frederic Frederic - 4 months ago 41
Swift Question

generating random game obstacles in swift

I have made 6 obstacles of UIImage.

These obstacles has their center.x moving -5 every 0.045 second based on my "movement" function. Therefor after sometime they are out of the picture.

I'm trying to figure out how I can make a function that regenerates these obstacles to comeback in the view from the right side and randomized, so my game keeps having obstacles scrolling in from the right side and everytime they scroll in from the right, they have a new center.y value.


Top of my mind, some sort of check in your movement function to see if center.x < 0 and if so, update center.x to be something larger than the right side of the screen and at the same time update center.y to a new random value.

Some vague pseudocode:

if < 0 - yourItem.size.width / 2 { // to make sure we're clear of the screen = CGPoint(x: maxWidth, y: randomYPos)