States not changing

I’m attempting to add turnbased MP in my game, and adding a secondary menu that allows the user(s) to decide if they want MP or single player, but the state is not changing, even when I change the starting state to the desired state. I am completely confused at this.

--The name of the project must match your Codea project name if dependencies are used. 
--Project: Tilt Island
--Version: Beta 1.1.3
--Comments:

supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
saveProjectInfo("Author","CodeaNoob")
saveProjectInfo("Description","Tilt the paddle to dodge the endless falling objects")
readProjectInfo("Author")
readProjectInfo("Description")
function setup()
parameter.watch("Lives")
parameter.text("Console","",callBack)
Lives = 3
GAME_PLAYING = 0
GAME_DEAD = 1
GAME_OVER = 2
GAME_SELECT = 3
state = GAME_DEAD
objectTable = {}
lastGenerated = ElapsedTime
interval = 0.12
num = 7
table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
pColor = color(0, 0, 0, 255)
paddle = Paddle(WIDTH/2,5,80,15,pColor)
end
function draw()
spriteMode(CENTER)
sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
pushStyle()    
spriteMode(CENTER)
fill(0, 0, 0, 255)
textMode(CENTER)
textAlign(CENTER)
font("Copperplate-Light")
fontSize(30)
text("Tap here to play", 500,275)
text("NEW! MP",500,200)
text("Version 1.2.0,500,750)
fontSize(50)
text("Tilt Island!", 500, 600)
fontSize(40)
text("Tilt the device to dodge the falling objects!", 500, 500)
paddle:draw()
if state == GAME_PLAYING then
spriteMode(CENTER)
sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
pushStyle()
randomObjectGenerate()   
popStyle()       
score = ElapsedTime 
fill(255)
font("Futura-CondensedExtraBold")
fontSize(30)
textMode(CORNER)
score = ElapsedTime - start
fill(255, 255, 255, 255)
text("Score: "..string.format("%.0f",score),10,HEIGHT-50)
text("Lives: "..Lives,900,HEIGHT-50)
if state == GAME_OVER then
alert("Nice job getting "..string.format("%.0f",score).." points!","Epic!")
state = GAME_DEAD
end
if state == GAME_SELECT then
sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
text("Turn-Based MP",100,275)
text("Single Player",600,275)
end
end
end

function randomObjectGenerate()
for k,v in pairs(objectTable) do 
v:draw()    
end
if ElapsedTime - lastGenerated > interval then
lastGenerated = ElapsedTime
num = num + 1
table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))  
end
for k,v in pairs(objectTable) do 
if paddle:collide(v.a.x,v.a.y) and v.a.info.id > 0 then
sound(SOUND_HIT, 46295)
v.a.info.id=0
Lives=Lives-1
if Lives<1 then
state=GAME_OVER

end
end
if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
table.remove(objectTable,k)
v.a:destroy()
v.a = nil
v=nil
end
end
end
function drawPaddle()
paddle:draw()
end
function touched(touch)
if touch.tapCount == 1 and state == GAME_DEAD then
state = GAME_SELECT
end
end
--The Paddle Tab
Paddle = class()
function Paddle:init(posx,posy,pWidth,pHeight,pColor)
self.pWidth = pWidth
self.pHeight = pHeight
self.posx = posx
self.posy = posy
pos = vec2(posx,posy)
size = vec2(pWidth,pHeight)
self.pColor = pColor
end
function Paddle:draw()
checkGrav()
fill(pColor)   
rect(pos.x,pos.y,self.pWidth,self.pHeight)
end
function Paddle:collide(x,y)
if x>pos.x and x<pos.x+self.pWidth and y>self.posy and y<self.posy+self.pHeight*2 then
return true
end
return false
end
--This Function checks the Gravity
function checkGrav()
if pos.x >= 0 and pos.x <= WIDTH - size.x then
if Gravity.x > 0 then
pos.x = pos.x + (20 * Gravity.x)
end  
if Gravity.x < 0 then
pos.x = pos.x - (-20 * Gravity.x)
end    
end 
if pos.x < 0 then
pos.x = 0
end
if pos.x > WIDTH - size.x then
pos.x = WIDTH - size.x
end
end
--The Object Tab
Objects = class()
function Objects:init(x,cat,id)
self.a = physics.body(CIRCLE,20)
self.a.x = x
self.a.y = HEIGHT
self.a.gravityScale = 1
self.a.restitution = 0.0
self.a.interpolate = true
self.asleepingAllowed = true
self.flag = true
self.category = cat
self.a.info = {}
self.a.info.id = id
end
function Objects:draw()
pushMatrix()
translate(self.a.x, self.a.y)
if self.category == 1 then
sprite("Planet Cute:Rock",0,0,50,88)
elseif self.category == 2 then
sprite("Platformer Art:Spikes",0,0,50,51)
elseif self.category == 3 then
sprite("Planet Cute:Brown Block",0,0,50,80)
elseif self.category == 4 then
sprite("Planet Cute:Plain Block",0,0,50,85)         

end
popMatrix()
end  
function Objects:touched(t)
end
-- add this function anywhere in your code

function callBack()
if Console == "Dumbwaytodie" then
close()
end
if Console == "Lives" then
Lives = math.huge
end
if Console == "Easy" then
interval = 1
end
if Console == "Regular" then
interval = .12
end
if Console == "Hard" then
interval = .1
end
if Console == "help" then
print("Lives = Infinite Lives")
print("Easy = Easy difficulty")
print("Regular = Regular Difficulty")
print("Hard = Hard Difficulty")
print("Insane = Dont even try this Cheat Code")
end
if Console == "Insane" then 
interval = 0.00001
end
if Console == "CodeaNoob" then
print("Thank you for playing this game!")
end
if Console == "Credits" then
print("Game made by CodeaNoob")
print("Major help by dave1707")
print("Paddle Class made by Josh Knox")
print("Have fun! :)")
end
if Console == "Roadmap" then
print("1.2.0: Planning to add Turn-Based MP, Possibly ABC Player Music")
end
if Console == "num" then
num = 21
end
end

@CodeaNoob Is this the way you write your code or was this post a mistake. It’s very difficult to look at someone else’s code when there is no indentation or spaces separating functions. As for your problem, near the end of the draw() function, after the line state = GAME_DEAD, add an end statement. Then after the line text(“Single Player”,600,275), remove one of the end statements.

@CodeaNoob, i agree with @dave1707. Proper indentation is key

Sorry, I dont indent, I like to have a nice,clean code

@CodeaNoob, indenting makes the code clean. The way it is right now it’s just a block of code that is a lot harder to read

Thanks, I will try to indent the code

Proper indentation is similar to this:

function ex()
    blah
    blah

    if blah then
         blah
    end
end

Example

Same error this time, sorry for the un-indented code

supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
saveProjectInfo("Author","CodeaNoob")
saveProjectInfo("Description","Tilt the paddle to dodge the endless falling objects")
readProjectInfo("Author")
readProjectInfo("Description")
function setup()
parameter.watch("Lives")
parameter.text("Console","",callBack)
Lives = 3
GAME_DEAD = 0
GAME_OVER = 1
GAME_SELECT = 2
SINGLE_PLAYER = 3
M_P = 4
state = GAME_DEAD
objectTable = {}
lastGenerated = ElapsedTime
interval = 0.12
num = 7
table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
pColor = color(0, 0, 0, 255)
paddle = Paddle(WIDTH/2,5,80,15,pColor)
end
function draw()
spriteMode(CENTER)
sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
pushStyle()    
spriteMode(CENTER)
fill(0, 0, 0, 255)
textMode(CENTER)
textAlign(CENTER)
font("Copperplate-Light")
fontSize(30)
text("Tap here to play", 500,275)
text("NEW! MP",500,200)
text("Version 1.2.0",500,750)
fontSize(50)
text("Tilt Island!", 500, 600)
fontSize(40)
text("Tilt the device to dodge the falling objects!", 500, 500)
paddle:draw()
if state == SINGLE_PLAYER then
spriteMode(CENTER)
sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
pushStyle()
randomObjectGenerate()   
popStyle()       
score = ElapsedTime 
fill(255)
font("Futura-CondensedExtraBold")
fontSize(30)
textMode(CORNER)
score = ElapsedTime - start
fill(255, 255, 255, 255)
text("Score: "..string.format("%.0f",score),10,HEIGHT-50)
text("Lives: "..Lives,900,HEIGHT-50)
if state == GAME_OVER then
alert("Nice job getting "..string.format("%.0f",score).." points!","Epic!")
state = GAME_DEAD
end
if state == GAME_SELECT then
sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
text("Turn-Based MP",187.5,275)
text("Single Player",WIDTH-187.5,275)
text("TILT ISLAND",WIDTH/2,HEIGHT-50)
drawPaddle()
end
end
end

function randomObjectGenerate()
for k,v in pairs(objectTable) do 
v:draw()    
end
if ElapsedTime - lastGenerated > interval then
lastGenerated = ElapsedTime
num = num + 1
table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))  
end
for k,v in pairs(objectTable) do 
if paddle:collide(v.a.x,v.a.y) and v.a.info.id > 0 then
sound(SOUND_HIT, 46295)
v.a.info.id=0
Lives=Lives-1
if Lives<1 then
state=GAME_OVER

end
end
if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
table.remove(objectTable,k)
v.a:destroy()
v.a = nil
v=nil
end
end
end
function drawPaddle()
paddle:draw()
end
function touched(touch)
if touch.tapCount == 1 and state == GAME_DEAD then
state = GAME_SELECT
end

if touch.x < WIDTH/2 and state == GAME_SELECT then
state = SINGLE_PLAYER
start = ElapsedTime
Lives = 3
end
if state == GAME_SELECT and touch.x > WIDTH/2 then
state = M_P
end
end
--The Paddle Tab
Paddle = class()
function Paddle:init(posx,posy,pWidth,pHeight,pColor)
self.pWidth = pWidth
self.pHeight = pHeight
self.posx = posx
self.posy = posy
pos = vec2(posx,posy)
size = vec2(pWidth,pHeight)
self.pColor = pColor
end
function Paddle:draw()
checkGrav()
fill(pColor)   
rect(pos.x,pos.y,self.pWidth,self.pHeight)
end
function Paddle:collide(x,y)
if x>pos.x and x<pos.x+self.pWidth and y>self.posy and y<self.posy+self.pHeight*2 then
return true
end
return false
end
--This Function checks the Gravity
function checkGrav()
if pos.x >= 0 and pos.x <= WIDTH - size.x then
if Gravity.x > 0 then
pos.x = pos.x + (20 * Gravity.x)
end  
if Gravity.x < 0 then
pos.x = pos.x - (-20 * Gravity.x)
end    
end 
if pos.x < 0 then
pos.x = 0
end
if pos.x > WIDTH - size.x then
pos.x = WIDTH - size.x
end
end
--The Object Tab
Objects = class()
function Objects:init(x,cat,id)
self.a = physics.body(CIRCLE,20)
self.a.x = x
self.a.y = HEIGHT
self.a.gravityScale = 1
self.a.restitution = 0.0
self.a.interpolate = true
self.asleepingAllowed = true
self.flag = true
self.category = cat
self.a.info = {}
self.a.info.id = id
end
function Objects:draw()
pushMatrix()
translate(self.a.x, self.a.y)
if self.category == 1 then
sprite("Planet Cute:Rock",0,0,50,88)
elseif self.category == 2 then
sprite("Platformer Art:Spikes",0,0,50,51)
elseif self.category == 3 then
sprite("Planet Cute:Brown Block",0,0,50,80)
elseif self.category == 4 then
sprite("Planet Cute:Plain Block",0,0,50,85)         

end
popMatrix()
end  
function Objects:touched(t)
end
-- add this function anywhere in your code

function callBack()
if Console == "Dumbwaytodie" then
close()
end
if Console == "Lives" then
Lives = math.huge
end
if Console == "Easy" then
interval = 1
end
if Console == "Regular" then
interval = .12
end
if Console == "Hard" then
interval = .1
end
if Console == "help" then
print("Lives = Infinite Lives")
print("Easy = Easy difficulty")
print("Regular = Regular Difficulty")
print("Hard = Hard Difficulty")
print("Insane = Dont even try this Cheat Code")
end
if Console == "Insane" then 
interval = 0.00001
end
if Console == "CodeaNoob" then
print("Thank you for playing this game!")
end
if Console == "Credits" then
print("Game made by CodeaNoob")
print("Major help by dave1707")
print("Paddle Class made by Josh Knox")
print("Have fun! :)")
end
if Console == "Roadmap" then
print("2.0: Planning to add Turn-Based MP, Possibly ABC Player Music")
print("3.0: Setting up a server where scores can be uploaded and saved and fetched by TiltIsland")
end
if Console == "num" then
num = 21
end
end

singleplayer should be assigned to a different variable, as state cannot be single player and something else.

I changed it to a different variable, but it still doesnt leave the GAME_DEAD state

supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
saveProjectInfo("Author","CodeaNoob")
saveProjectInfo("Description","Tilt the paddle to dodge the endless falling objects")
readProjectInfo("Author")
readProjectInfo("Description")

function setup()
    parameter.watch("Lives")
    parameter.text("Console","",callBack)
    Lives = 3
    GAME_DEAD = 0
    GAME_OVER = 1
    GAME_SELECT = 2
    SINGLE_PLAYER = 3
    M_P = 4
    state = GAME_DEAD
    objectTable = {}
    lastGenerated = ElapsedTime
    interval = 0.12
    num = 7
    table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
    pColor = color(0, 0, 0, 255)
    paddle = Paddle(WIDTH/2,5,80,15,pColor)
end

function draw()
    spriteMode(CENTER)
    sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    pushStyle()    
    spriteMode(CENTER)
    fill(0, 0, 0, 255)
    textMode(CENTER)
    textAlign(CENTER)
    font("Copperplate-Light")
    fontSize(30)
    text("Tap here to play", 500,275)
    text("NEW! MP",500,200)
    text("Version 1.2.0",500,750)
    fontSize(50)
    text("Tilt Island!", 500, 600)
    fontSize(40)
    text("Tilt the device to dodge the falling objects!", 500, 500)
    paddle:draw()
    
    if state == SINGLE_PLAYER then
        spriteMode(CENTER)
        sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
        pushStyle()
        randomObjectGenerate()   
        popStyle()       
        score = ElapsedTime 
        fill(255)
        font("Futura-CondensedExtraBold")
        fontSize(30)
        textMode(CORNER)
        score = ElapsedTime - start
        fill(255, 255, 255, 255)
        text("Score: "..string.format("%.0f",score),10,HEIGHT-50)
        text("Lives: "..Lives,900,HEIGHT-50)
        
        if state == GAME_OVER then
            alert("Nice job getting "..string.format("%.0f",score).." points!","Epic!")
        end
        
        if state == GAME_SELECT then
            sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
            text("Turn-Based MP",187.5,275)
            text("Single Player",WIDTH-187.5,275)
            text("TILT ISLAND",WIDTH/2,HEIGHT-50)
            drawPaddle()
        end
    end
end

function randomObjectGenerate()
    for k,v in pairs(objectTable) do 
        v:draw()    
    end
    
    if ElapsedTime - lastGenerated > interval then
        lastGenerated = ElapsedTime
        num = num + 1
        table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))  
    end
    
    for k,v in pairs(objectTable) do 
        if paddle:collide(v.a.x,v.a.y) and v.a.info.id > 0 then
            sound(SOUND_HIT, 46295)
            v.a.info.id=0
            Lives=Lives-1
            
            if Lives<1 then
                state=GAME_OVER
            end
        end
    
        if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
            table.remove(objectTable,k)
            v.a:destroy()
            v.a = nil
            v=nil
        end
    end
end

function drawPaddle()
    paddle:draw()
end

function touched(touch)
    if touch.tapCount == 1 and state == GAME_DEAD then
        state = GAME_SELECT
    end
    
    if touch.x < WIDTH/2 and state == GAME_SELECT then
        state = SINGLE_PLAYER
        start = ElapsedTime
        Lives = 3
    end
    
    if state == GAME_SELECT and touch.x > WIDTH/2 then
        state = M_P
    end
end

--The Paddle Tab
Paddle = class()

function Paddle:init(posx,posy,pWidth,pHeight,pColor)
    self.pWidth = pWidth
    self.pHeight = pHeight
    self.posx = posx
    self.posy = posy
    pos = vec2(posx,posy)
    size = vec2(pWidth,pHeight)
    self.pColor = pColor
end

function Paddle:draw()
    checkGrav()
    fill(pColor)   
    rect(pos.x,pos.y,self.pWidth,self.pHeight)
end

function Paddle:collide(x,y)
    if x>pos.x and x<pos.x+self.pWidth and y>self.posy and y<self.posy+self.pHeight*2 then
        return true
    end
    
    return false
end

--This Function checks the Gravity
function checkGrav()
    if pos.x >= 0 and pos.x <= WIDTH - size.x then
        if Gravity.x > 0 then
            pos.x = pos.x + (20 * Gravity.x)
        end  
        
        if Gravity.x < 0 then
            pos.x = pos.x - (-20 * Gravity.x)
        end    
    end 

    if pos.x < 0 then
        pos.x = 0
    end

    if pos.x > WIDTH - size.x then
        pos.x = WIDTH - size.x
    end
end

--The Object Tab
Objects = class()

function Objects:init(x,cat,id)
    self.a = physics.body(CIRCLE,20)
    self.a.x = x
    self.a.y = HEIGHT
    self.a.gravityScale = 1
    self.a.restitution = 0.0
    self.a.interpolate = true
    self.asleepingAllowed = true
    self.flag = true
    self.category = cat
    self.a.info = {}
    self.a.info.id = id
end

function Objects:draw()
    pushMatrix()
    translate(self.a.x, self.a.y)
    
    if self.category == 1 then
        sprite("Planet Cute:Rock",0,0,50,88)
    elseif self.category == 2 then
        sprite("Platformer Art:Spikes",0,0,50,51)
    elseif self.category == 3 then
        sprite("Planet Cute:Brown Block",0,0,50,80)
    elseif self.category == 4 then
        sprite("Planet Cute:Plain Block",0,0,50,85)         
    end
    
    popMatrix()
end  

function Objects:touched(t)
    
end
-- add this function anywhere in your code

function callBack()
    output.clear()
    
    if Console == "Dumbwaytodie" then
        close()
    end
    
    if Console == "Lives" then
        Lives = math.huge
    end
    
    if Console == "Easy" then
        interval = 1
    end
    
    if Console == "Regular" then
        interval = .12
    end
    
    if Console == "Hard" then
        interval = .1
    end
    
    if Console == "Insane" then 
        interval = 0.00001
    end
    
    if Console == "CodeaNoob" then
        print("Thank you for playing this game!")
    end
    
    if Console == "Credits" then
        print("Game made by CodeaNoob")
        print("Major help by dave1707")
        print("Paddle Class made by Josh Knox")
        print("Have fun! ")
    end
    
    if Console == "Roadmap" then
        print("2.0: Planning to add Turn-Based MP, Possibly ABC Player Music")
        print("3.0: Setting up a server where scores can be uploaded and saved and fetched by TiltIsland")
    end
    
    if Console == "num" then
        num = 21
    end
    
        
    if Console == "help" then
        print("Dumbwaytodie = End game")
        print("Lives = Infinite Lives")
        print("Easy = Easy difficulty")
        print("Regular = Regular Difficulty")
        print("Hard = Hard Difficulty")
        print("Insane = Dont even try this Cheat Code")
        print("CodeaNoob = thanks")
        print("Credits = Game credits")
        print("Roadmap = Future plans")
    end
end

I indented and spaced your code to make it cleaner

This problem is probably very obvious, I dont see it though. U got anything @JakAttak

@CodeaNoob I’m not sure what you’re trying to do, but if you tap on the left side of the screen you set state=SINGLE_PLAYER. If you tap the right side you set state=M_P. Once you’re in state M_P, there isn’t anyway out and you do nothing in draw() except show the menu.

@dave1707 This si the unfinished code, I just typed up what I had so far, and tested it. My main problem is getting out if state = GAME_DEAD(Main Menu)

@CodeaNoob If I tap the left side of the screen, the game starts and I can play it. But once the game ends, the state is now GAME_OVER. There isn’t any code to get out of GAME_OVER. One thing you need to do in the draw() function is to move paddle:draw() to the end of the draw() function so it’s the last thing executed in draw().