I have a question about the bonus lesson 13 challenge. I got a decent amount of the way into completing this challenge before needing to refer to the solution for assistance. What I don’t understand is: Why do the first 3 @State vars all need to equal 1 in this situation? And in my HStack where I have the 3 assets show up: In the parenthesis, why does it have to be a string?
import SwiftUI
struct ContentView: View {
@State var gameFruit = 1
@State var middleMan = 1
@State var mostRight = 1
@State var creditAmount = 1000
var body: some View {
VStack {
Spacer()
Text("Swift UI Slots!")
.font(.title)
.padding(.all)
Spacer()
HStack {
Text("Credits: \(creditAmount)")
.font(.body)
}
Spacer()
HStack {
Spacer()
Image("fruit\(gameFruit)")
.resizable()
.aspectRatio(contentMode: .fit)
.padding(.all)
Spacer()
Image("fruit\(middleMan)")
.resizable()
.aspectRatio(contentMode: .fit)
.padding(.all)
Spacer()
Image("fruit\(mostRight)")
.resizable()
.aspectRatio(contentMode: .fit)
.padding(.all)
Spacer()
}
Spacer()
Button("Spin"){
gameFruit = Int.random(in: 1...3)
middleMan = Int.random(in: 1...3)
mostRight = Int.random(in: 1...3)
if (gameFruit == middleMan && middleMan == mostRight) {
creditAmount += 5
} else {
creditAmount -= 5
}
}
.padding()
.padding([.leading, .trailing], 30)
.background(Color.red)
.font(.title)
.foregroundColor(Color.white)
.cornerRadius(25)
Spacer()
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}