如何生成不快速重叠的随机按钮

时间:2021-04-26 15:26:12

标签: swift

嗨,我是 Swift 语言的新手,所以我正在尝试创建一个泡泡流行游戏,所以我想要创建的泡泡基本上是一个 UI 按钮,所以我会随机生成泡泡,我想要做的是我想生成彼此不重叠的气泡

这是我的代码

@objc func generateBubble() {
        let bubble = Bubble()
        bubble.animation()
        let xPosition = Int.random(in: Int(viewBubble.frame.minX) + 30 ... Int(viewBubble.frame.maxX) - 30)
        let yPosition = Int.random(in: Int(viewBubble.frame.minY) + 50 ... Int(viewBubble.frame.maxY) - 50)
        bubble.addTarget(self, action: #selector(bubblePressed), for: .touchUpInside)
        bubble.frame = CGRect(x: xPosition, y: yPosition, width: 50, height: 50)
        bubble.layer.cornerRadius = 0.5 * bubble.bounds.size.width
        self.view.addSubview(bubble)
    }
    

    
    @IBAction func bubblePressed(_ sender: UIButton) {
        if Bubble().backgroundColor == UIColor.red{
            self.score += 1;
            
        }
        else if Bubble().backgroundColor == UIColor.green{
            self.score += 5;
            
        }
        if Bubble().backgroundColor == UIColor.black{
            self.score += 10;
            
        }
        if Bubble().backgroundColor == UIColor.blue{
            self.score += 8;
            
        }
        
        sender.removeFromSuperview()
    }

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

实现此结果的一种方法是遍历屏幕中的所有气泡,并根据此调整新气泡的 X 和 Y。

例如,您可以创建一个函数来检查是否有重叠:

func checkOverlap(x: CGFloat, y: CGFloat, view: UIView, offset: CGFloat) -> Bool{
        for case let bubble as UIButton in view.subviews {
            let current_x = bubble.frame.origin.x
            let current_y = bubble.frame.origin.y

            if((x < current_x + offset && x > current_x) && (y < current_y + offset && y > current_y)){
                  //OVERLAP
                  return true
            }
        }
        // NO OVERLAP
        return false
    }

那么你可以这样使用这个函数:

    @objc func generateBubble() {
        let bubble = Bubble()
        bubble.animation()
        // I suggest to use CGFloat.random
        let xPosition = CGFloat.random(in: viewBubble.frame.minX + 30 ... viewBubble.frame.maxX - 30)
        let yPosition = CGFloat.random(in: viewBubble.frame.minY + 50 ... viewBubble.frame.maxY - 50)
        
        // Offset is bubbleWidth/2      
        while(checkOverlap(x: xPosition, y: yPosition, view: self.view, offset: 25)){
        // Here you can calculate x and y randomly again or you can
        // add offset if you want, or any other calculation you want to move the 
        // new bubble
        // Example 1, just get new random x and y:
          xPosition = CGFloat.random(in: viewBubble.frame.minX + 30 ... viewBubble.frame.maxX - 30)
          yPosition = CGFloat.random(in: viewBubble.frame.minY + 50 ... viewBubble.frame.maxY - 50)

        // Example 2, move x and/or y by offset, or other number with - or +:
           xPosition += 25 // or xPosition -= 25, or you can choose randomly
           yPosition += 25 // or yPosition -= 25, or you can choose randomly

        // Example 3, random guess in any combination you want:
         if(CGFloat.random(in: 0 ... 10) > 5){
            xPosition += 25
            yPosition -= 25
         }else{
            xPosition -= 25
            yPosition += 25
          }
        }

        bubble.addTarget(self, action: #selector(bubblePressed), for: .touchUpInside)
        bubble.frame = CGRect(x: xPosition, y: yPosition, width: 50, height: 50)
        bubble.layer.cornerRadius = 0.5 * bubble.bounds.size.width
        self.view.addSubview(bubble)
    }
<块引用>

注意:

请注意,因为while循环在最坏的情况下可能会永远循环,所以我建议对其执行次数设置限制,例如通过设置 maxIteration = 50,并在 while 循环中:

    var i = 0 
    while(checkOverlap(...) && i < maxIteration){ 
      //doStuff 
      i += 1
     } 
相关问题