How can find the missing bracket in this code?
Asked Answered
B

2

5

When I run the code it tells me there's an error which is ')' expected near '=':

function restartLvl()
    for i = 1, #balloonTexts do
        display.remove(balloonTexts[i])
        print ("restart level")
    end

    score.text = '0'
    ballRemain.text = '3'
    balloonText = {}
    createBalloons(1, 3)

    if (askUser.isVisible = true) then  --this is the line where the error occured
        askUser.isVisible = false
    end

    if (yesBtn.isVisible = true) then
        yesBtn.isVisible = false
    end

    if (noBtn.isVisible = true) then
        noBtn.isVisible = false
    end
end

I don't know how it is still missing a ')', because I closed all the brackets.

Basswood answered 11/3, 2014 at 8:10 Comment(0)
R
7

= is the assignment operator, == is the operator to test equality. Change it to:

if (askUser.isVisible == true) then
    askUser.isVisible = false
end

And all the others as well. The brackets () can be ommited for simplicity:

if askUser.isVisible == true then
    askUser.isVisible = false
end

If the value is a boolean, you can also do this because all values that are not nil or false are treated as true.

if askUser.isVisible then
    askUser.isVisible = false
end
Rodolphe answered 11/3, 2014 at 8:13 Comment(4)
Strike "If the value is a boolean".Riptide
It works as well if the value of NOT a boolean. However, it's not exactly equivalent to the condition in the question (tests explicitly if the value is true).Rodolphe
Yes, you're right. The mentioning of nil threw me off because nil is not boolean. Your statement is nuanced but correct.Riptide
The stylistic pattern that the question asker uses "expression == true" and "expression == false" is a curious but not uncommon one. Perhaps it should be avoided in Lua. In general, the only time I see it making sense is where it improves readability but in most such cases, renaming a field, function or variable would be a better way. if askUser.isVisible then reads just fine to me.Riptide
C
0

This is not related to your answer but

I recommend you to use lua glider IDE because this type error can be detect well by using this IDE.

Cash answered 14/3, 2014 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.