When you call alert(lose)
in ansQuestion()
, Lua interprets lose
as a variable, not a string. Since the variable does not exist, it is nil, so the second branch of the if (state == 'win')
gets executed. It so happens that in the case of "lose", this is the branch you want, but it is just luck. When you do alert(win)
, win is also a variable so it is nil, so the same branch gets executed, and that time it is the wrong branch. Use
alert('lose')
and
alert('win')
as you did in ballCollision
listener. Note that there are several lines of duplicate code in ansQuestion and alert.