Having problems with global variables... Please help D:

Hello! I’m currently developing a game.

Here’s what I want to do: a select level screen with three options: stage 1, stage 2, stage 3.

At first, stage 2 and stage 3 should not be accessible, being both texts with alpha 0.5.

I created a global boolean with “false” value.

In the select stage screen I made this condition:

if *global boolean* == true then

    stage2 text.alpha = 1.0

end

and, aftewards, I added a function so the stage 2 text would change to the game’s second stage.

there’s a collision at the end of stage 1, where i set the global boolean to true, but it doesn’t seem to be 

working, as i moved to the stage selection screen and stage 2’s alpha would still be 0.5. 

can you tell me what’s wrong? 

Without more code, including what you named the global (it can matter), where you set the value (it matters) and some context as to where the function setting the global is called, I’m not sure anyone can help =(

Oh, sorry.

So here’s how it is:

a toMenu.lua file that loads the menu. There’s where the globa is creates. Here’s the code:

module (…, package.seeall)

local _W = display.contentWidth/2

local _H = display.contentHeight/2

local sprite = require(“sprite”)

function new()

    local loadGroup = display.newGroup()

    local spriteSheet = sprite.newSpriteSheet(“LoadingCircle.png”, 102, 102) 

    local spriteSet = sprite.newSpriteSet(spriteSheet, 1,8)

    sprite.add(spriteSet, “loading”, 1, 8, 600, 0)

    

    stage1completed = false

    local holdie = sprite.newSprite(spriteSet)

    holdie.x = _W

    holdie.y = _H

    loadGroup:insert(holdie)

    holdie:prepare(“loading”)

    holdie:play()

    function changeScene(event)

        director:changeScene(“menu”, “moveFromRight”)

    end    

    timer.performWithDelay(2000, changeScene, 1)

    return loadGroup

end    

here’s the file where the options of stages are:

module (…, package.seeall)

local _W = display.contentWidth/2

local _H = display.contentHeight/2

function new()

    local selectGroup = display.newGroup()

    local stage1 = display.newText(“Stage 1”, 50, 140, “Helvetica”, 45)

    selectGroup:insert(stage1)

    local stage2 = display.newText(“Stage 2”, 300, 140, “Helvetica”, 45)

    stage2.alpha = 0.5

    selectGroup:insert(stage2)

    local stage3 = display.newText(“Stage 3”, 550, 140, “Helvetica”, 45)

    stage3.alpha = 0.5

    selectGroup:insert(stage3)

    if stage1completed == true then

        stage2.alpha = 1.0

    end

    local tapback = display.newText(“Tap to go back”, 250, 400, “Helvetica”, 40)

    function backtomenu(event)

        if event.phase == “ended” then

            director:changeScene(“menu”, “fade”)

        end    

    end

    tapback:addEventListener(“touch”, backtomenu)    

    function goStage1(event)

        if event.phase == “ended” then

            director:changeScene(“stage1”, “fade”)

        end    

    end

    stage1:addEventListener(“touch”, goStage1)        

    return selectGroup

end    

and here’s the collision function on stage1.lua file, where i set the global to “true”:

function finishCollision(event)

        --variáveis para comparação de scores

        stage1completed = true

        local comparePl – player

        local compareEn – inimigo

        

        --if que testa colisão com o fim da fase

        if event.phase == “began” then

            keepOnRunning = false --pausa o x_update

            counting = false --pausa o cronômetro

            red:pause(“running”) – pausa a sprite de corrida

            jump_red = false --desabilita o botão de corrida

            --cria tela preta com alpha sobre a fase    

            local darkgame = display.newRect(red.x-800,red.y-800,1800,1000)

            darkgame:setFillColor(0, 0, 0)

            darkgame.alpha=0.8

            menuGroup:insert(darkgame)

            

            --cria tela branca com alpha, onde estarão os scores e botões 

            scoreScreen = display.newRect(red.x-50, red.y-250,500,400) 

            scoreScreen.alpha=0.7

            menuGroup:insert(scoreScreen)

            

            --cria botão que recarrega a fase

            local reloadStage = display.newRect(scoreScreen.x-200, scoreScreen.y+100, 100,50)

            reloadStage:setFillColor(0, 255,0)

            menuGroup:insert(reloadStage)

            --função do botão de recarregar fase

            function backToStage(event)

                if event.phase == “ended” then

                        director:changeScene(“reload1”, “fade”)

                end    

            end    

            reloadStage:addEventListener(“touch”, backToStage)

            --cria botão para recarregar o menu

            local reloadMenu = display.newRect(scoreScreen.x-20, scoreScreen.y+100, 50,50)

            reloadMenu:setFillColor(100,100,0)

            menuGroup:insert(reloadMenu)

            --função do botão de recarregar menu

            function toReloadMenu(event)

                if event.phase == “ended” then

                    director:changeScene(“select_1”, “fade”)

                end

            end

            reloadMenu:addEventListener(“touch”, toReloadMenu)        

                

            --texto de score do player

            local p1_score = display.newText(txt_counter.text, red.x+200, red.y-200, “Helvetica”, 50) --cria um texto com o tempo da personagem

            p1_score:setTextColor(0, 0, 0)

            menuGroup:insert(p1_score)

            

            --texto “your time”, que ficará ao lado do score do player

            local your_score = display.newText(“Your time:”, red.x-50, red.y-200, “Helvetica”, 50)

            your_score:setTextColor(0,0,0)

            menuGroup:insert(your_score)

            --texto de score do inimigo

            local enemy1_score = display.newText(“30”, red.x+200, red.y-100, “Helvetica”, 50) --cria um texto com o tempo da inimigo

            enemy1_score:setTextColor(0, 0, 0)

            menuGroup:insert(enemy1_score)

            

            --texto “E1’s time”, que ficará ao lado do score do inimigo

            local yourscoreE1 = display.newText(“E1’s time:”, red.x-50, red.y-100, “Helvetica”, 50)

            yourscoreE1:setTextColor(0,0,0)

            menuGroup:insert(yourscoreE1)

            

            --aqui a variável do player receberá o valor do timer e a variável do inimigo receberá um valor pré-programado

            comparePl = number

            compareEn = 30

            

            --comparação de variáveis

            --se o score do player for menor que o do inimigo, o botão para mudar de fase é disponível. Caso contrário, aparece o botão para recarregar o stage

            if comparePl < compareEn then

                local changeStage = display.newRect(scoreScreen.x+100, scoreScreen.y+100, 100, 50)

                changeStage:setFillColor(255, 0, 255)

                menuGroup:insert(changeStage)

                

                --função que move o plaer para a fase seguinte, ao clicar no botão

                function moveToStage(event)

                    if event.phase == “ended” then

                        director:changeScene(“stage2”, “fade”)

                    end    

                end    

                changeStage:addEventListener(“touch”, moveToStage)        

            end    

        end    

    end    

is this enough?

Without more code, including what you named the global (it can matter), where you set the value (it matters) and some context as to where the function setting the global is called, I’m not sure anyone can help =(

Oh, sorry.

So here’s how it is:

a toMenu.lua file that loads the menu. There’s where the globa is creates. Here’s the code:

module (…, package.seeall)

local _W = display.contentWidth/2

local _H = display.contentHeight/2

local sprite = require(“sprite”)

function new()

    local loadGroup = display.newGroup()

    local spriteSheet = sprite.newSpriteSheet(“LoadingCircle.png”, 102, 102) 

    local spriteSet = sprite.newSpriteSet(spriteSheet, 1,8)

    sprite.add(spriteSet, “loading”, 1, 8, 600, 0)

    

    stage1completed = false

    local holdie = sprite.newSprite(spriteSet)

    holdie.x = _W

    holdie.y = _H

    loadGroup:insert(holdie)

    holdie:prepare(“loading”)

    holdie:play()

    function changeScene(event)

        director:changeScene(“menu”, “moveFromRight”)

    end    

    timer.performWithDelay(2000, changeScene, 1)

    return loadGroup

end    

here’s the file where the options of stages are:

module (…, package.seeall)

local _W = display.contentWidth/2

local _H = display.contentHeight/2

function new()

    local selectGroup = display.newGroup()

    local stage1 = display.newText(“Stage 1”, 50, 140, “Helvetica”, 45)

    selectGroup:insert(stage1)

    local stage2 = display.newText(“Stage 2”, 300, 140, “Helvetica”, 45)

    stage2.alpha = 0.5

    selectGroup:insert(stage2)

    local stage3 = display.newText(“Stage 3”, 550, 140, “Helvetica”, 45)

    stage3.alpha = 0.5

    selectGroup:insert(stage3)

    if stage1completed == true then

        stage2.alpha = 1.0

    end

    local tapback = display.newText(“Tap to go back”, 250, 400, “Helvetica”, 40)

    function backtomenu(event)

        if event.phase == “ended” then

            director:changeScene(“menu”, “fade”)

        end    

    end

    tapback:addEventListener(“touch”, backtomenu)    

    function goStage1(event)

        if event.phase == “ended” then

            director:changeScene(“stage1”, “fade”)

        end    

    end

    stage1:addEventListener(“touch”, goStage1)        

    return selectGroup

end    

and here’s the collision function on stage1.lua file, where i set the global to “true”:

function finishCollision(event)

        --variáveis para comparação de scores

        stage1completed = true

        local comparePl – player

        local compareEn – inimigo

        

        --if que testa colisão com o fim da fase

        if event.phase == “began” then

            keepOnRunning = false --pausa o x_update

            counting = false --pausa o cronômetro

            red:pause(“running”) – pausa a sprite de corrida

            jump_red = false --desabilita o botão de corrida

            --cria tela preta com alpha sobre a fase    

            local darkgame = display.newRect(red.x-800,red.y-800,1800,1000)

            darkgame:setFillColor(0, 0, 0)

            darkgame.alpha=0.8

            menuGroup:insert(darkgame)

            

            --cria tela branca com alpha, onde estarão os scores e botões 

            scoreScreen = display.newRect(red.x-50, red.y-250,500,400) 

            scoreScreen.alpha=0.7

            menuGroup:insert(scoreScreen)

            

            --cria botão que recarrega a fase

            local reloadStage = display.newRect(scoreScreen.x-200, scoreScreen.y+100, 100,50)

            reloadStage:setFillColor(0, 255,0)

            menuGroup:insert(reloadStage)

            --função do botão de recarregar fase

            function backToStage(event)

                if event.phase == “ended” then

                        director:changeScene(“reload1”, “fade”)

                end    

            end    

            reloadStage:addEventListener(“touch”, backToStage)

            --cria botão para recarregar o menu

            local reloadMenu = display.newRect(scoreScreen.x-20, scoreScreen.y+100, 50,50)

            reloadMenu:setFillColor(100,100,0)

            menuGroup:insert(reloadMenu)

            --função do botão de recarregar menu

            function toReloadMenu(event)

                if event.phase == “ended” then

                    director:changeScene(“select_1”, “fade”)

                end

            end

            reloadMenu:addEventListener(“touch”, toReloadMenu)        

                

            --texto de score do player

            local p1_score = display.newText(txt_counter.text, red.x+200, red.y-200, “Helvetica”, 50) --cria um texto com o tempo da personagem

            p1_score:setTextColor(0, 0, 0)

            menuGroup:insert(p1_score)

            

            --texto “your time”, que ficará ao lado do score do player

            local your_score = display.newText(“Your time:”, red.x-50, red.y-200, “Helvetica”, 50)

            your_score:setTextColor(0,0,0)

            menuGroup:insert(your_score)

            --texto de score do inimigo

            local enemy1_score = display.newText(“30”, red.x+200, red.y-100, “Helvetica”, 50) --cria um texto com o tempo da inimigo

            enemy1_score:setTextColor(0, 0, 0)

            menuGroup:insert(enemy1_score)

            

            --texto “E1’s time”, que ficará ao lado do score do inimigo

            local yourscoreE1 = display.newText(“E1’s time:”, red.x-50, red.y-100, “Helvetica”, 50)

            yourscoreE1:setTextColor(0,0,0)

            menuGroup:insert(yourscoreE1)

            

            --aqui a variável do player receberá o valor do timer e a variável do inimigo receberá um valor pré-programado

            comparePl = number

            compareEn = 30

            

            --comparação de variáveis

            --se o score do player for menor que o do inimigo, o botão para mudar de fase é disponível. Caso contrário, aparece o botão para recarregar o stage

            if comparePl < compareEn then

                local changeStage = display.newRect(scoreScreen.x+100, scoreScreen.y+100, 100, 50)

                changeStage:setFillColor(255, 0, 255)

                menuGroup:insert(changeStage)

                

                --função que move o plaer para a fase seguinte, ao clicar no botão

                function moveToStage(event)

                    if event.phase == “ended” then

                        director:changeScene(“stage2”, “fade”)

                    end    

                end    

                changeStage:addEventListener(“touch”, moveToStage)        

            end    

        end    

    end    

is this enough?