Это код, который я хочу сделать в игре, но деньги go меньше 0. Если вы хотите, чтобы мне не хватало денег, я создаю if money - cost> 0, а if self.money - self.cost> 0.
struct ContentView: View {
@State var money = 0
@State var cost = 1
@State var addToTap = 1
var body: some View {
VStack(alignment: .center, spacing: 5.0){
Text("money = \(money)")
Text("$/tap=\(self.addToTap)")
Spacer()
.frame(height: 50)
Button(action: {
self.money += self.addToTap
}) {
Text(" Tap! ")
.font(.title)
.fontWeight(.medium)
.foregroundColor(.blue)
.overlay(
RoundedRectangle(cornerRadius:20)
.stroke(Color.blue, lineWidth: 3))
}
Button(action: {
if <#condition#> {
self.cost += 1;
self.money -= self.cost;
self.addToTap += 1
} else {
<#statements#>
}
}) {
Text(" Buy! ")
.font(.title)
.fontWeight(.medium)
.foregroundColor(.red)
.overlay(
RoundedRectangle(cornerRadius:20)
.stroke(Color.red, lineWidth: 3))
}
}
}
}
Спасибо:)