碰撞后如何阻止比分上升超过1?

时间:2019-12-18 12:16:46

标签: ios swift collision

创建一个愤怒的小鸟类型的项目,我使球在目标上飞行,并且在碰撞后目标消失了,但是即使在将球从子视图中移除后,当球在目标框架中时,得分也不断提高。

  let enemy1 = UIImageView(image: nil)
        enemy1.image = UIImage(named: "enemy2.png")
        enemy1.frame = CGRect(x: w*0.85, y: h*0.035, width: w*0.12, height: h*0.22)
        self.view.addSubview(enemy1)



 collisionBehaviour.action = {
        for boulderView in self.bouldersArray{
            if boulderView.frame.intersects(enemy1.frame){
                enemy1.removeFromSuperview()
                self.scoreCount += 1
                self.scoreCountLabel.text = "Score: \(self.scoreCount)"

1 个答案:

答案 0 :(得分:0)

一种方法可能是检查敌人1视图是否仍然存在:

collisionBehaviour.action = {
                if enemy1.superview != nil {
                    //your code
                    for boulderView in self.bouldersArray {
                    }
                }
        }