Code Troubleshooting

Hi, I’m 13 and new to coding. I more/less taught myself to do basic coding in Codea, with the help of a few tutorials. I’m working on a basic game as my first project, but I’ve come across a problem with restarting the game after my game is finished (not that the app is crashing, code issue). Basically, the way my app works is: main > menu > game > gameover then back to game. The main problem I’m having is in gameover i have a Boolean variable (self.touchReady) and when it changes to true, Gameover:touched(touch) doesnt recognize the change in self.touchReady. Any idea of a solution to that problem? Here’s the code.


--# Main
-- Tilt Dodger

function setup()
   displayMode (FULLSCREEN) 
    supportedOrientations (LANDSCAPE_ANY)
    menu = Menu()
    gameover = Gameover()
  --  clearLocalData ()
end


function draw()
menu:draw()
end

function touched(touch)
if touch.state == BEGAN then
    gameover:touched(touch)
    menu:touched (touch)
    end
    
end

--# Menu
Menu = class()

function Menu:init()
self.start = false
    rungame = Game()
end

function Menu:draw()
   if self.start == false then
    background (0, 3, 255, 255)
        font("Copperplate")
    fontSize (70)
        fill(255, 171, 0, 255)
        text ("Tap Anywhere to Start", WIDTH /2, HEIGHT /2)
        sprite("Cargo Bot:Codea Icon", WIDTH/2, 240, 120, 120)
        sprite("Cargo Bot:Codea Logo", WIDTH /2, 115, 220, 75 )
        font("Copperplate-Light")
        fontSize (35)
        text ("Made With", WIDTH /2, 170)
        font("Copperplate-Light")
        fill(198, 255, 0, 255)
        fontSize (125)
        text ("TILT  DODGER", WIDTH /2, HEIGHT - 200)
  
         else
        rungame:draw()
    end
end

function Menu:touched(touch)
 if touch.state == BEGAN then
        self.start = true
    end
end

--# Game
Game = class()

function Game:init()
    player = Player()
    enemy = Enemy()
    gaov = Gameover()
    hs = Highscore()
    self.gameover = false
    self.startset = false
end

function Game:draw()  
  if self.startset ==  false then  
        player:init()
        hs:set()
        hs:init()
        enemy:init()
    end
    self.startset = true
    background(255, 240, 0, 255)
    hs:draw()
if self.gameover == false then
        font("Copperplate-Light")
        fill(0, 0, 0, 255)
        fontSize (35)
        text (hs.currenttime, WIDTH /2, HEIGHT -50)
          player:draw()
        
        enemy:draw()
        distToHero = player.position:dist(enemy.position)
        
        if distToHero < 100 then
        self.gameover = true
        hs.Endtime = ElapsedTime
        hs.calculate = true
        end
        
    else

         gaov:draw()
        
        

    end
    


end





--# Player
-- Player
Player = class()

function Player:init()
zFix = Rotationratefix()
 self.position = vec2 (WIDTH /2, HEIGHT /2)
zFix:draw()
     self.calx = zFix.newloc.x
    self.caly = zFix.newloc.y
end

function Player:draw()
 zFix:draw()
    sprite("Documents:ExampleCircle", self.position.x, self.position.y, 200, 200)
    self.gravx = zFix.newloc.x - self.calx
    self.position.x = self.position.x + self.gravx * 45
    self.gravy = zFix.newloc.y - self.caly
    self.position.y = self.position.y + self.gravy * 45
    
    if self.position.x > WIDTH -50 then
    self.position.x = WIDTH -50
    end
    if self.position.y > HEIGHT -50 then
        self.position.y = HEIGHT -50
    end
    if self.position.x < 50 then
        self.position.x = 50
    end
    if self.position.y < 50 then 
        self.position.y = 50
    end
end

function Player:touched(touch)
    -- Codea does not automatically call this method
end

--# Enemy
-- enemy
Enemy = class()

function Enemy:init()
self.position = vec2 (WIDTH - 50, HEIGHT - 40)
    self.speed = vec2 (6,6)
    
end

function Enemy:draw()
sprite("Tyrian Remastered:Rock Boss Right", self.position.x, self.position.y, 100, 100)
    self.position.x = self.position.x + self.speed.x
    self.position.y = self.position.y + self.speed.y
    
    if self. position.x > WIDTH - 50 then
        
        if self.speed.x < 30 then
        self.speed.x = self.speed.x * -1.15
            else 
            self.speed.x = self.speed.x * -1
    end
        end
            if self. position.y > HEIGHT - 50 then
        self.position.y = HEIGHT - 50
        if self.speed.y < 30 then
        self.speed.y = self.speed.y * -1.10
            else 
            self.speed.y = self.speed.y * -1
    end
        end
                if self. position.x < 50 then
        if self.speed.x > -30 then
        self.speed.x = self.speed.x * -1.15
            else 
            self.speed.x = self.speed.x * -1
    end
        end
                    if self. position.y < 50 then
        if self.speed.y > -30 then
        self.speed.y = self.speed.y * -1.10
            else 
            self.speed.y = self.speed.y * -1
    end
        end
end


--# Highscore
Highscore = class()

function Highscore:init()
    saveLocalData ("curscore", 0)
    self.startTime = ElapsedTime
    self.Endtime = 0
    self.calculate = false
    self.currenth = readLocalData ("Highscore", 0) 
    print ("highscore init complete")
end
function Highscore:set()
    self.startTime = ElapsedTime
    end
function Highscore:draw()
if self.calculate == true then
        self.score = self.Endtime - self.startTime
        print (self.score)
      --  text (self.score, WIDTH /2, HEIGHT /2 +100)
        if self.score > self.currenth then
        his = self.score
        saveLocalData ("Highscore", his)
            end
            else
           self.currenttime = ElapsedTime - self.startTime
    end
    saveLocalData ("curscore", self.score)

end

function Highscore:touched(touch)
    -- Codea does not automatically call this method
end

--# Gameover

Gameover = class()

function Gameover:init()
    hs = Highscore()
    self.touchReady = false
self.rerun = false
end



function Gameover:draw()
   print (self.rerun) 
    if self.rerun == false then
    background(255, 0, 4, 255)
    font("Courier")
    fontSize (50)
    fill(0, 252, 255, 255)
    text ("Score", WIDTH/2, HEIGHT /2 + 150 )
        text (readLocalData ("curscore",0), WIDTH/2, HEIGHT /2 + 100)
    text ("High Score", WIDTH/2, HEIGHT/2 -100)
    text (readLocalData ("Highscore", 0), WIDTH/2, HEIGHT /2 -150)
    font("Copperplate")
    fontSize(70)
    fill(5, 255, 0, 255)
    text ("Game Over, tap to retry", WIDTH /2, HEIGHT /2 )
else
     Game:init()  
    Game:draw()
        end
end


function Gameover:touched(touch)
print ("received")
    if self.touchReady == true then
    self.rerun = true
    end
end


--# Rotationratefix
Rotationratefix = class()

function Rotationratefix:init()
   self.rotationx = 0
    self.rotationydunno = 0 
end

function Rotationratefix:draw()
    self.rotationx = self.rotationx + RotationRate.y /30
    self.rotationydunno = self.rotationydunno + RotationRate.x /30
    self.rotationy = self.rotationydunno - self.rotationydunno *2
    self.newloc = vec2 (self.rotationx, self.rotationy)
end



--# Hsbackup
--[[
Gameover = class()

function Gameover:init()
    hs = Highscore()
    self.touchReady = false
self.rerun = false
end



function Gameover:draw()
   print (self.rerun) 
    if self.rerun == false then
    background(255, 0, 4, 255)
    font("Courier")
    fontSize (50)
    fill(0, 252, 255, 255)
    text ("Score", WIDTH/2, HEIGHT /2 + 150 )
        text (readLocalData ("curscore",0), WIDTH/2, HEIGHT /2 + 100)
    text ("High Score", WIDTH/2, HEIGHT/2 -100)
    text (readLocalData ("Highscore", 0), WIDTH/2, HEIGHT /2 -150)
    font("Copperplate")
    fontSize(70)
    fill(5, 255, 0, 255)
    text ("Game Over, tap to retry", WIDTH /2, HEIGHT /2 )
else
     Game:init()  
    Game:draw()
        end
end


function Gameover:touched(touch)
print ("received")
         if touch.state == BEGAN and self.touchReady == true then
    self.rerun = true
    end
end
    
]]

Thanks

@Mr_Ninja I looked at your code and I didn’t see anywhere where you were setting self.touchReady to true. In GameOver:touched you’re checking if it’s true before setting self.rerun.

Ok, thanks. But when i set

self.touchReady = true

in Gameover:draw(), and add the line

 print (self.touchReady) 

in Gameover:touched, it prints false. Same thing as if I add

 gaov.touchReady = true

in Game:draw after the game. Is finished and calls gameover. Does this have to do with the fact that I am using self.____ rather than a local variable or something else? Thanks

I just noticed something that may be the problem, in touched(touch) you’re calling gameover:touched(touch), but the instance of Gameover is called gaov.

Edit: There are two instances of Gamover, gameover and gaov. You’re setting gaov’s touchReady to true, but asking gameover if its touchReady is true.

Thank you, that fixed it, just need to work out a few final bugs now and the app should work! Thanks