Lesson 13 problem

Hi everybody, I was doing the lesson 13 and i wanted to push a little more so i added another button credit button like if you were putting cash in the slot machine.
My problem is: I want when my credit is at 0 that is not possible to spin again because now without credit i can continue to spin and i don’t want that
I can’t found a solution
I am sure for you guys it will be easy but i am new dev

import SwiftUI

struct ContentView: View {
   
   @ State private var Left = "slot1"
   @ State private var Middle = "slot2"
   @ State private var Right = "slot3"
   @ State private var Score = 0
   @ State private var creditScore = 0
   var body: some View {
   	
   		VStack{
   			
   			Text("Swift Slot")
   				.font(.largeTitle)
   			Spacer()
   			HStack{
   			Text("Score:")
   			Text(String(Score))
   			}
   			
   			Text("Credits:")
   			Text(String(creditScore))
   			Spacer()
   			
   			HStack{
   				Spacer()
   			Image(Left)
   					.resizable().aspectRatio(contentMode: .fit)
   			Image(Middle)
   					.resizable().aspectRatio(contentMode: .fit)
   			Image(Right)
   					.resizable().aspectRatio(contentMode: .fit)
   			Spacer()
   			}
   			
   				Spacer()
   			
   			Button("Spin") {
   				
   				let slotLeft = Int.random(in: 1...3)
   				let slotMiddle = Int.random(in: 1...3)
   				let slotRight = Int.random(in: 1...3)
   				
//					update the slot
   				Left = "slot" + String(slotLeft)
   				Middle = "slot" + String(slotMiddle)
   				Right = "slot" + String(slotRight)
   				
   				
//					update the score
   				
   		if (slotLeft == slotMiddle) && (slotMiddle == slotRight) && (slotRight == slotLeft){
   					Score += 100
   					creditScore += 3

   				}else if (creditScore >= 1) && (Score <= 1){
   						creditScore -= 1
   						
   				}else if creditScore >= 1 {
   					Score -= 5
   					creditScore -= 1
   				}

   			}
   			.padding(.all)
   			.frame(width: 150.0)
                               .background(Color.green)
   			.cornerRadius(50)
   	                .foregroundColor(.white)
   		
   			Button("Credit"){
   				creditScore += 10	
   			}
   			.padding(.all)
   			.frame(width: 150.0)
   			.background(Color.red)
   			.cornerRadius(50)
   	                .foregroundColor(.white)
   		}
}

struct ContentView_Previews: PreviewProvider {
   static var previews: some View {
       ContentView()
   }
}
}

thanks

On the closing brace of your Spin button, add the modifier:

.disabled(condition)

where condition is a test to return either true or false. If the condition is true then the Button is disabled.

In your case the condition could be creditScore < 1

1 Like

Thank you, I can’t believe it was only a line of code.
I really couldn’t find the answer
that works well
thank you

1 Like