123ZDP
123ZDP

Reputation: 137

Specific Image Spawn

How can I edit this code so the spawnRandomPosition function also doesn't allow the image to spawn 45 pixels down from the bottom of the screen? So the top 45 pixels of the screen cannot be spawned in.

class SecondViewController: UIViewController {

private var addOne = 0

func spawnRandomPosition() -> CGPoint
{                
    let width  = UIScreen.main.fixedCoordinateSpace.bounds.width
    let height = UIScreen.main.fixedCoordinateSpace.bounds.height

    let centerArea = CGRect(x: UIScreen.main.fixedCoordinateSpace.bounds.midX - 75.0 / 2,
                            y: UIScreen.main.fixedCoordinateSpace.bounds.midY - 75.0 / 2,
                            width: 75.0,
                            height: 75.0)

    while true
    {
        let randomPosition = CGPoint(x: Int(arc4random_uniform(UInt32(width))), y: Int(arc4random_uniform(UInt32(height))))


        if !centerArea.contains(randomPosition)
        {
            return randomPosition
        }
    }
}

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
    view.center = CGPoint(x:view.center.x + translation.x,
                          y:view.center.y + translation.y)
     }
recognizer.setTranslation(CGPoint.zero, in: self.view)

if (WhiteDot.frame.contains(smallDot.frame) && smallDot.image != nil) {
    addOne += 1
    score.text = "\(addOne)"
    smallDot?.center = spawnRandomPosition()

}
}
    }

Upvotes: 0

Views: 92

Answers (2)

Christian Serrano
Christian Serrano

Reputation: 486

Maybe you need a different approach, is not elegant but you could use if's

func spawnRandomPosition() -> CGPoint  {

let screenWidth  = UIScreen.main.fixedCoordinateSpace.bounds.width
let screenHeight = UIScreen.main.fixedCoordinateSpace.bounds.height
boolean valid = true;
repeat
{
   let randomPosition = CGPoint(x:CGFloat(arc4random()),y:arc4random())
   if(randomPosition.x >screenWidth || randomPosition.y < 45 || (randomPosition.x > ((screenWidth/2)- 37.5) && randomPosition.x< ((screenWidth/2)+ 37.5)  || (randomPosition.y > ((screenHeight/2)- 37.5) && randomPosition.y< ((screenHeight/2)+ 37.5))
      valid = false
}while(valid = false;)
}

But of course you should take in consideration that could arc4random() give back a very high number and end up with an inefficient solution, so you could use rand() instead

Upvotes: 1

D. Mika
D. Mika

Reputation: 2808

Just modify function spawnRandomPosition a bit:

func spawnRandomPosition() -> CGPoint
{
    let minY: CGFloat = 45.0

    let width  = UIScreen.main.fixedCoordinateSpace.bounds.width
    let height = UIScreen.main.fixedCoordinateSpace.bounds.height - minY

    let centerArea = CGRect(x: UIScreen.main.fixedCoordinateSpace.bounds.midX - 75.0 / 2,
                            y: UIScreen.main.fixedCoordinateSpace.bounds.midY - 75.0 / 2,
                            width: 75.0,
                            height: 75.0)

    while true
    {
        let randomPosition = CGPoint(x:CGFloat(arc4random()).truncatingRemainder(dividingBy: height),
                                     y:minY + CGFloat(arc4random()).truncatingRemainder(dividingBy: width))

        // Check for 'forbidden' area
        if !centerArea.contains(randomPosition)
        {
            return randomPosition
        }
    }
}

Upvotes: 1

Related Questions