Game states

Can anyone tell me how to do gamestates?

this is the basic, very simple, I did so for you to understand, there are many ways to implement it…

function setup()
state = 1
end
function draw()

if state == 1 then
--Do whatever you want in this state
end

if state == 2
--Do whatever you want in this state
end

end

EDIT: Also, it appears this thread was post about 15 minutes after your other one. Please A: Try to spend at least an hour trying to figure it out yourself. B: Ask your problem on the other post you made. C: Use the forums search button. I

http://twolivesleft.com/Codea/Talk/discussion/comment/29113
http://twolivesleft.com/Codea/Talk/discussion/3955/simple-scene-selecter-state-machine#Item_20
that’s 2 handy links :wink:

 -- GameStates
 function setup()
 print("GAMESTATE EXAMPLE")
 PhysicsBodies = {}

 z = example2()

 page2 = Buttons(WIDTH/2,HEIGHT/2,"TOUCH HERE",nextPage)
 end

 -- This function gets called once every frame
 function draw()
-- This sets a dark background color 
 background(40, 40, 50)
 strokeWidth(5)
  page2:draw()
 if GameState == "NEXTPAGE" then --> if state is new, change
    z:draw()
 end
 end

 function touched(touch)
 page2:touched(touch)
  if GameState == "NEXTPAGE" then
  -- z.touched(touch)
 end
 end


 --> FUNCTION TO CALL IN BUTTONS
 function nextPage()
 -->THIS IS THE BASIC
  -->CALL CHANGE STATE TO CHANGE THE PAGE
 ChangeState("NEXTPAGE",z.init)
 end

 -----------------------------------
 --> THIS FUNCTION CHANGE THE STATE
 -----------------------------------
 function ChangeState(State,funct)
 for k,v in pairs(PhysicsBodies) do
  v:destroy()
 v=nil
 end
  PhysicsBodies={}
 funct()
 GameState=State  --> the original state
 end
  ------------------
  --> ANOTHER CLASS
  ------------------
 example2 = class()
 
 function example2:init()
 end
 
 function example2:draw()
  background(255)
  text("New Page Using Change State(Game States)",WIDTH/2,HEIGHT/2)
 end
 
 Buttons = class()
 --> A SIMPLE BUTTON CLASS
 function Buttons:init(x,y,txt,funct,Mode,l,h)
 self.x = x
 self.y = y
 fontSize(30)
 font("Georgia-Bold")
 self.txt = txt
 self.Sl,self.Sh = textSize(self.txt)
 self.funct = funct
 self.l=l or self.Sl+20
 self.h=h or self.Sh+20
 self.mode=Mode
 self.colour=color(175, 158, 44, 255)

 end
 function Buttons:draw()
 pushStyle()
 fontSize(30)
 font("Copperplate-Bold")
 fill(self.colour)
  if self.mode then
    text(self.txt,self.x+self.l/2,self.y+self.h/2)
 else
    text(self.txt,self.x,self.y)
 end
  popStyle()
  end

  function Buttons:touched(touch)
   if self.mode then
    self.tx=touch.x-self.l/2
    self.ty=touch.y-self.h/2
  else
    self.tx = touch.x
    self.ty = touch.y
 
  end

  if self.tx<=self.x + self.l/2 and self.tx>=self.x - self.l/2 and 
   self.ty>=self.y - self.h/2 and self.ty<=self.y + self.h/2 then
        if touch.state==BEGAN then
            self.colour=color(251, 251, 251, 255)
            self.press=true
        elseif touch.state == ENDED and self.press then
            self.colour=color(255, 255, 255, 255)
            self.funct()
            sound(SOUND_PICKUP, 89899)
        end
 else
    self.colour=color(0, 0, 0, 255)
 end
 if touch.state==ENDED then
    self.colour=color(0, 0, 0, 255)
    self.press=false
 end
 end

http://coolcodea.wordpress.com/2013/05/04/47-game-template/

Explains zoyt’s template

@MCordova18 Here’s another example of changing states. This example changes states based on where you press on the screen. In an actual game, the game state would change based on what happens in the game.


displayMode(FULLSCREEN)

function setup()
    gameState=gameState1    
end

function draw()
    background(40,40,50)
    fill(255)
    gameState()
end

function gameState1()
    background(172, 78, 78, 119)
    sprite("Planet Cute:Character Boy",WIDTH/2,800)
    fontSize(30)
    text("This is gamestate 1",WIDTH/2,700) 
    showText()
end

function gameState2()
    background(95, 173, 101, 119)
    sprite("Planet Cute:Character Cat Girl",WIDTH/2,800)
    fontSize(30)
    text("This is gamestate 2",WIDTH/2,700) 
    showText()
end

function gameState3()
    background(77, 124, 172, 119)
    sprite("Planet Cute:Character Horn Girl",WIDTH/2,800)
    fontSize(30)
    text("This is gamestate 3",WIDTH/2,700) 
    showText()
end

function gameState4()
    background(172, 77, 170, 119)
    sprite("Planet Cute:Character Pink Girl",WIDTH/2,800)
    fontSize(30)
    text("This is gamestate 4",WIDTH/2,700) 
    showText()
end

function showText()
    fontSize(20)
    text("You can do whatever you want in this state",WIDTH/2,650)
    text("Press here for gamestate 1",WIDTH/2,400)
    text("Press here for gamestate 2",WIDTH/2,325)
    text("Press here for gamestate 3",WIDTH/2,250)
    text("Press here for gamestate 4",WIDTH/2,175)    
end

function touched(t)
    if t.y>375 and t.y<425 then
        gameState=gameState1
    end
    if t.y>300 and t.y<350 then
        gameState=gameState2
    end
    if t.y>225 and t.y<275 then
        gameState=gameState3
    end
    if t.y>150 and t.y<200 then
        gameState=gameState4
    end
end

Here is my modified version of something @Briarfox posted:


--# Main
--The name of the project must match your Codea project name if dependencies are used. 
--Project: Scene Selecter
--Version: Alpha 1.0
--Comments: 

function setup()
    --Name your scenes
    Scene = SceneManager()
    
    Scene:addScene("menu", Title)
    Scene:addScene("play", Game)
    Scene:addScene("end", End)
    
    Scene:change("menu")
end

function draw()
    Scene:draw()
end

function touched(touch)
    Scene:touched(touch) 
end

--# Scene
SceneManager = class()

function SceneManager:init()
    self.viewx = 0
    self.touchAllowed = true
    
    self:addScene("__i", {})    -- Create blank instance in case no scenes are added
end

function SceneManager:addScene(id, inst)
    if not self.scenes then self.scenes = {} end
    
    self.scenes[id] = inst
    
    if self.scenes[id].init then
        self.scenes[id]:init()
    end
    
    self.currentScene = id
end


--Change scene
function SceneManager:change(name)
    if self.scenes[name] ~= nil then
        self.nextScene = name
        self:transition()
    end
end

function SceneManager:transition()
    self.touchAllowed = false
    
    tween(0.75, self, {viewx = -WIDTH}, tween.easing.bounceOut, 
    function()
        self:trueChange()
    end)
end

function SceneManager:trueChange()
    if self.scenes[self.currentScene].onExit then
        self.scenes[self.currentScene]:onExit()
    end
    
    collectgarbage()
    
    self.currentScene = self.nextScene
    
    if self.scenes[self.currentScene].onEnter then
        self.scenes[self.currentScene]:onEnter()
    end

    self.nextScene = nil
    self.touchAllowed = true
    
    self.viewx = 0
    
    collectgarbage()
end


function SceneManager:draw()
    pushStyle()
    pushMatrix()
    
    translate(self.viewx, 0)
    
    if self.scenes[self.currentScene].draw then
        self.scenes[self.currentScene]:draw()
    end
    
    translate(WIDTH, 0)
    
    if self.nextScene ~= nil and self.scenes[self.nextScene].draw then
        self.scenes[self.nextScene]:draw()
    end
    
    pushStyle()
    strokeWidth(5) stroke(0)
    line(0, 0, 0, HEIGHT)
    popStyle()
    
    popMatrix()
    popStyle()
end

function SceneManager:touched(t)
    if self.touchAllowed and self.scenes[self.currentScene].touched then
        self.scenes[self.currentScene]:touched(t)
    end
end

function SceneManager:keyboard(k)
    if self.scenes[self.currentScene].keyboard then
        self.scenes[self.currentScene]:keyboard(k)
    end
end

function SceneManager:orientationChanged(o)
    if self.scenes[self.currentScene].orientationChanged then
        self.scenes[self.currentScene]:orientationChanged(o)
    end
end


function maxn(t)
    local amnt = 0
    if type(t) == "table" then
        for i,v in pairs(t) do
            amnt = amnt + 1
        end
    end
    
    return amnt
end



--# Title
Title = class()

function Title:init(x)
end

function Title:draw()
    background(255, 255, 255, 255)
    font("Baskerville-Bold")
    fontSize(40)
    fill(0, 36, 255, 255)
    text("Title Screen....\
(Tap to Change)", WIDTH/2, HEIGHT/2)
end

function Title:touched(t)
    if t.state == ENDED then 
        Scene:change("play") 
    end
end


--# Game

Game = class()

function Game:init(x)

end

function Game:draw()
    background(255, 255, 255, 255)
    font("Baskerville-Bold")
    fontSize(40)
    fill(0, 36, 255, 255)
    text("Game Screen....\
(Tap to Change)", WIDTH/2, HEIGHT/2)
end

function Game:touched(touch)
    if touch.state == ENDED then 
        Scene:change("end") 
    end
end



--# End

End = class()

function End:init()
    
end

function End:draw()
    background(255, 255, 255, 255)
    font("Baskerville-Bold")
    fontSize(40)
    fill(0, 36, 255, 255)
    text("Game Over Screen....\
(Tap to Change)", WIDTH/2, HEIGHT/2)
end

function End:touched(touch)
    if touch.state == ENDED then 
        Scene:change("menu") 
    end
end

Very short

MENU = 0
PLAY = 1
state = MENU
function setup()
if state = MENU then
print("Menu")
else 
print(Playing")
end