【问题标题】:Button function not handling the event properly when pressed?按下时按钮功能无法正确处理事件?
【发布时间】:2021-05-15 17:41:52
【问题描述】:
struct Question {
    var Question : String!
    var Answers : [String]!
    var AnswerNumber : Int!
}

class LevelOne: SKScene {
    
    var button = Button()
    
    var buttons = [Button]()
    let Question_met = UILabel(frame: CGRect(x: 0.3, y: 0.3, width: 40, height: 21))
    
    var Questions = [Question]()
    
    var QNumber = Int()
    
    var buttonNames = [""]
    
    var AnsNumber = Int()
    
    let selectors:[Selector] = [#selector(handleButton1), #selector(handleButton2), #selector(handleButton3),#selector(handleButton4)]
    
    override func didMove(to view: SKView) {
        
        let background = SKSpriteNode(imageNamed: "background")
        background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
        background.zPosition = 0
        self.addChild(background)
        
        QuestionFunction()
        
        ButtonFuction()
         
        PickQuestion()
        
    }
    
    @objc func QuestionFunction(){
        
        Questions = [Question(Question: "What is a BMW ?", Answers: ["Bat","Cat","Vehicle","Pat"], AnswerNumber: 2),
        Question(Question: "What is a Boeing ?", Answers: ["Bat","Aircraft","Cat","Pat"], AnswerNumber: 1),
        Question(Question: "What is a Ant ?", Answers: ["Insect","Cat","Insect","Nate"], AnswerNumber: 2),
        Question(Question: "What is a Apple ?", Answers: ["Fruit","Cat","bat","Pat"], AnswerNumber: 0),
        Question(Question: "Where is london ?", Answers: ["UK","USA","France","Germany"], AnswerNumber: 0),
        Question(Question: "Where is New York ?", Answers: ["Canada","USA","France","Germany"], AnswerNumber: 2),
        Question(Question: "where is Berlin ?", Answers: ["UK","USA","Italy","Germany "], AnswerNumber: 3),
        Question(Question: "Where is Toronto ?", Answers: ["India","Africa","Canada","Norway"], AnswerNumber: 2),
        Question(Question: "Where is Rome ?", Answers: ["Japan","China","Italy","Ireland"], AnswerNumber: 2),
        Question(Question: "where is Chennai ?", Answers: ["India","Brazil","Swiss","Germany"], AnswerNumber: 0),]
        
        Question_met.frame = CGRect(x: 330, y: 70, width: 200, height: 21)
        Question_met.backgroundColor = UIColor.orange
        Question_met.textColor = UIColor.white
        Question_met.textAlignment = NSTextAlignment.center
        Question_met.text = "What caused Global Warming ?"
        self.view?.addSubview(Question_met)
        
    }
    
    @objc func ButtonFuction(){
        
        let stacView = UIStackView()
        stacView.spacing = 12
        stacView.distribution = .fillEqually
        stacView.axis = .horizontal
        stacView.translatesAutoresizingMaskIntoConstraints = false
        view!.addSubview(stacView)
        
        buttonNames = ["One","Two","Three","Four"]
        
        for name in buttonNames {
            button = Button()
            button.setTitle(name, for: .normal)
            stacView.addArrangedSubview(button)
            buttons.append(button)
        }
        
        for i in 0..<4{
            
            button.addTarget(self, action: selectors[i], for: .touchUpInside)
            
        }
    
        NSLayoutConstraint.activate([stacView.centerXAnchor.constraint(equalTo: view!.centerXAnchor),stacView.centerYAnchor.constraint(equalTo: view!.centerYAnchor),stacView.widthAnchor.constraint(equalToConstant: 350),stacView.heightAnchor.constraint(equalToConstant:70)])
        
    }
    
    @objc func PickQuestion(){
        
        if Questions.count > 0{
            QNumber = 0
            Question_met.text = Questions[QNumber].Question
            
            AnsNumber = Questions[QNumber].AnswerNumber
            
            for i in 0..<buttons.count{
                buttons[i].setTitle(Questions[QNumber].Answers[i], for: .normal)
            }
            Questions.remove(at: QNumber)
        }
        else {
            print("Done!")
        }
    }
    
    @objc func handleButton1() {
        
        if AnsNumber == 0 {
            PickQuestion()
            print("Correct!")
        }
        
        else {
            print("You are Wrong!!!")
        }
    }
    
    @objc func handleButton2(){
        
        if AnsNumber == 1 {
            PickQuestion()
            print("Correct!")
        }
        
        else {
            print("You are Wrong!!!")
        }
        
    }
    
    @objc func handleButton3(){
        
        if AnsNumber == 2 {
            PickQuestion()
            print("Correct!")
        }
        
        else {
            print("You are Wrong!!!")
            
        }
        
    }
    
    @objc func handleButton4(){
        
        if AnsNumber == 3 {
            PickQuestion()
            print("Correct!")
        }
        
        else {
            print("You are Wrong!!!")
        }
        
    }
}

按钮操作方法在知道按下哪个正确答案按钮方面似乎并不准确,而每个问题的答案分配了不同的数字,这些方法似乎只有在按下最后一个按钮时才会向前移动。

提前感谢您的帮助。

【问题讨论】:

    标签: swift xcode button sprite-kit skspritenode


    【解决方案1】:

    您的问题位于您的ButtonFunction():

    for name in buttonNames {
        button = Button()
        button.setTitle(name, for: .normal)
        stacView.addArrangedSubview(button)
        buttons.append(button)
    }
    
    for i in 0..<4{
        
        button.addTarget(self, action: selectors[i], for: .touchUpInside)
        
    }
    

    在第二个循环中,您没有初始化button 变量,所以这始终是第四个按钮。 最好像这样在一个循环中加入这段代码:

    for i in 0..<4{
        button = Button()
        button.setTitle(buttonNames[i], for: .normal)
        stacView.addArrangedSubview(button)
        buttons.append(button)
        button.addTarget(self, action: selectors[i], for: .touchUpInside)
    }
    

    除了这个解决方案之外,还有一些关于你的编码风格的建议:

    • 为了方便和更好的可读性,函数名和变量名应以小写字母开头
    • 在尽可能小的范围内声明变量。
      • buttonbuttonNamesselectors 仅在 ButtonFunction() 中使用,因此应在此处声明。
      • button 的声明实际上应该只在 for 循环中。这样,编译器会告诉你,button 在你的第二个循环中是未知的,你会自己看到问题;)

    【讨论】:

    • 非常感谢您的帮助。解决方案奏效了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 2015-08-29
    • 2018-09-01
    • 2018-10-12
    • 2021-11-15
    相关资源
    最近更新 更多