Reputation: 37
hi i'm making a game but i can't get an if state to work
now i added the full code of the program (contentView.swift)
hope its more useful to help me
the code is made in xcode and the program is for macos
coded in xcode whit swiftui
plz do not use this code its copyrighted by me
import SwiftUI
struct ContentView: View {
@State private var sp = false
@State private var loaded = false
@State private var uname = ""
@State private var text = "input a number"
@State private var etext = ""
@State private var sats = ""
@State private var mbt = "select collor"
@State private var gameState = 0
@State private var data = 0
@State private var temp = 0
@State private var type = 0
@State private var collor = 0
@State private var intsats = 0
//@State private var password = ""
var body: some View {
VStack {
if sp == false {
Text("Alien entertaiment presentate\n\n")
Text("casino\n\n")
Button(action: {
self.sp = true
//self.loadEndings()
//self.load()
}) {
Text("Start")
}
Text("\n\n")
}else if sp == true && loaded == false {
TextField("user name", text: $uname)
//SecureField("pasword", text: $password)
Text(uname)
//Text(password)
Button(action: {
self.loaded = true
self.load()
}) {
Text("sign in")
}
}else if sp == true && loaded == true {
Text("\(data)")
Text("\n")
Text(etext)
if etext != "" {Text("\n")}
Text("\n")
//Text(text)
Text("\n")
if gameState == 0 {
MenuButton(mbt) {
Button("red", action: {self.mbt = "red"; self.collor = 1})
Button("black", action: {self.mbt = "red"; self.collor = 2})
}
TextField("a number", text: $sats)
Button(action: {
self.intsats = Int(self.sats) ?? 0
if self.intsats == 0 || self.intsats > self.data {
self.etext = "not valid number or you do not have enuf mony"
}else{
self.gameState = 1
}
}) {
Text("bet")
}
}else if gameState == 1
{
if self.rand() == self.collor
{ //Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
self.text = "you won"
self.data = 1
}
}
}
}
}
let defaults = UserDefaults.standard
func save()
{
defaults.set(self.data, forKey: uname)
}
func load()
{
self.data = defaults.integer(forKey: uname)
self.temp = data
if temp == 0 {
self.data = 86
}
}
func rand() -> Int
{
var type = 0
type = Int.random(in: 1..<3)
return type
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
ops just filing
the game is for mac os catalina and can be use for probobly all appels stuff that can have apps on it
Upvotes: 2
Views: 13490
Reputation: 54611
This is because in the body
of the View you need to return a view. You can't just perform calculations etc. like in a normal function.
You can remove this code (which doesn't return a View and thus the compilation is failing):
else if gameState == 1 {
if self.rand() == self.collor {
self.text = "you won"
self.data = 1
}
}
and place it in you button's action:
if gameState == 0 {
...
TextField("a number", text: $sats)
Button(action: {
self.intsats = Int(self.sats) ?? 0
if self.intsats == 0 || self.intsats > self.data {
self.etext = "not valid number or you do not have enuf mony"
} else {
self.gameState = 1
if self.rand() == self.collor { // <- move it here
self.text = "you won"
self.data = 1
}
}
}) {
Text("bet")
}
}
Upvotes: 4