How to compare Variable values ?

I’m creating a game and I need to compare to values of set variables. The code I have written for this game is

local correctAnswer = 1

local numberOfTries = 0

local correctText = display.newText( “Good Job” ,200,400,nil, 50 )

correctText.isVisible = false

correctText:setTextColor( 255, 255, 255)


–My button touch to detemine the Game answer

 local function AnswerOneTouch(touch)

–moving image answerOne

    if (touch.phase==“begin”) then   

        local gameAnswer = 1

    end

    if (touch.phase==“moved”) then 

    end

    if (touch.phase==“ended”) then  

    transition.to( answerNumberOne, { time=800, x=(answerSix.x ), y=(answerSix.y)})

    print (“touch”)

    

  

    return true

    end

end  


if correctAnswer == gameAnswer then

correctText.isVisible = True

end

–Incorrect Answer

if correctAnswer ~= gameAnswer then

print(“Bad Job”)

numberOfTries = numberOfTries + 1 

end

My problem is, it doesn’t work when pressed my button that I set to be the right Answer. My button 1 is suppose set the Game Answer to 1 and I set the Correct Answer to 1 in the beginning. Can someone please help ?

   

   if (touch.phase==“begin”) then   

        local gameAnswer = 1

   end

Declaring the local in the function AnswerOneTouch means the variable (including the value) doesn’t exist outside of the local function AnswerOneTouch

So would I make a Global Variable ? I’m not to sure about how to solve this problem.

Forward reference gameAnswer like you have done with:

local correctAnswer = 1

local numberOfTries = 0

At the top of the file.

Variables are scoped to the “blocks” that they’re contained within - by forward referencing you’re putting the scope of the variable outside of that block/funciton - so it’s “visible” to all areas.

That said - if you really want to learn a better method I’d suggest looking at the nature of tables as objects in Lua, it will help to avoid Spaghetti code and also any future problems such as local variable limits.

Also note API changes to colors and coloring of display.newText… Unless you’re on an older build or using G1 compatibility your 

correctText:setTextColor( 255, 255, 255)

needs to change to 

correctText:setFillColor( 1, 1, 1)

   if (touch.phase==“begin”) then   

        local gameAnswer = 1

   end

Declaring the local in the function AnswerOneTouch means the variable (including the value) doesn’t exist outside of the local function AnswerOneTouch

So would I make a Global Variable ? I’m not to sure about how to solve this problem.

Forward reference gameAnswer like you have done with:

local correctAnswer = 1

local numberOfTries = 0

At the top of the file.

Variables are scoped to the “blocks” that they’re contained within - by forward referencing you’re putting the scope of the variable outside of that block/funciton - so it’s “visible” to all areas.

That said - if you really want to learn a better method I’d suggest looking at the nature of tables as objects in Lua, it will help to avoid Spaghetti code and also any future problems such as local variable limits.

Also note API changes to colors and coloring of display.newText… Unless you’re on an older build or using G1 compatibility your 

correctText:setTextColor( 255, 255, 255)

needs to change to 

correctText:setFillColor( 1, 1, 1)