skip to Main Content

I want to implement questions in my math game. The problem is, while I have set up the questions, I want to cap how many questions there are to 10. Instead of abiding by the circular progress bar when it reaches maximum, it keeps going (which I don’t want to happen). Please look over my code down below…

Question generator ->>

func answerIsCorrect(answer: Int) {
        let isCorrect = answer == correctAnswer ? true : false
        
        if isCorrect {
            self.score += Double(0.1)
        } else {
            self.score = score
        }
    }
    func generateAnswers() {
        firstNumber = Int.random(in: 0...(difficulty/2))
        secondNumber = Int.random(in: 0...(difficulty/2))
        var answerList = [Int]()
        
        correctAnswer = firstNumber + secondNumber
        
        for _ in 0...2 {
            answerList.append(Int.random(in: 0...difficulty))
        }
        
        answerList.append(correctAnswer)
        
        choiceArray = answerList.shuffled()
    }

Circular progress bar

Circle()
                                .trim(from: 0.01, to: CGFloat(score))
                            // when it reaches 1 (100%), show a view...
                                .stroke(LinearGradient(gradient: Gradient(colors: [Color.white, Color.white.opacity(0.2)]), startPoint: .topLeading, endPoint: .bottomTrailing), style: StrokeStyle(lineWidth: 17.5, lineCap: .round))
                                .shadow(radius: 8)
                                .rotationEffect(.degrees(90))
                                .rotation3DEffect(Angle(degrees: 180), axis: (x: 1, y: 0, z: 110))
                                .frame(width: 60, height: 60)
                                .animation(.easeOut)
                                .padding()
                                .padding([.top, .leading])

Thanks for looking!

2

Answers


  1. The circular progress bar is just a view, it will not perform an action just because the circle is complete. If you want something to happen when you reach 10 correct answers, you need to set up a condition somewhere.

    For example, in your func answerIsCorrect, after you change the score you can add a condition to trigger an action:

    if self.score == 1 {
        // something happens, like changing a @State var
    }
    
    Login or Signup to reply.
  2. you have to check here:
    showFinishedViewshould be some @State that e.g. shows a sheet or another view.

    func answerIsCorrect(answer: Int) {
        let isCorrect = (answer == correctAnswer)
        
        if isCorrect {
            self.score += Double(0.1)
        } else {
            self.score = score
        }
        // check here
        if self.score > 0.9 { showFinishedView = true }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search