Swift:当对象在区域内时运行代码

时间:2016-02-22 05:17:16

标签: ios xcode swift

我正在制作一个真假游戏,我们必须确定逻辑表达式是真还是假。这是一键式游戏。如果我们点击,当前的真实条件会变为false,反之亦然。一系列逻辑表达式将弹出屏幕,当它到达区域时,当前条件必须等于逻辑表达式才能获得分数。为了验证真值,它检查真值的图像。我试过一个案例,它是“真实的”,当前条件是真的,这是真的。但是,当它通过该区域时,得分没有增加。我哪里出错了?

@IBAction func button_clicked(sender: UIButton) {
    if (truth_click == true) {
        truth_click = false
        self.truth_button.image = UIImage(named: "false_button")
    }
    else if (truth_click == false) {
        truth_click = true
        self.truth_button.image = UIImage(named: "true_button")
    }
}

func check_truth() {
    if (truth_click == true) {
        //when it reaches the zone
        if (left_truth.center.y > 330 ) {

            //if true and true
            if ((left_truth.image == UIImage(named: "true")) && (symbol.image == UIImage(named: "and")) && (right_truth.image == UIImage(named: "true"))) {
                self.score += 1

            }
            //if true and false

            //if false and true

            //if false and false

            self.score_label.text = String(self.score)
        }
    }

    if (truth_click == false) {
        //when it reaches the zone
        if (left_truth.center.y > 330 ) {

            //if true and false
            if ((left_truth.image == UIImage(named: "true")) && (symbol.image == UIImage(named: "and")) && (right_truth.image == UIImage(named: "false")))  {
                self.score += 1

            }
            //if true and true

            //if false and true

            //if false and false

            self.score_label.text = String(self.score)
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您的部分问题是针对*_truth.image的新实例测试UIImage(named:...)。 UIImage是一个对象。实例化新的UIImage会创建一个新对象。具有相同内容的两个对象不相等......没有额外的工作。 enter image description here