Question

When players lose the game the gameFun function is called but for some reason when i switch alert(win) to gameFun and the alert(lose) in processAnswer to alert(win), it doesn't work. My aim is to ask users a question after they won or lost the game.

--ball collides with balloon
function ballCollision(e)
    if (e.other.name == 'balloon') then
       --ball collision codes
    end

    if(target.text == '0') then
        alert('win')
    end
end

function checkGameover()
    print( "Check game over" )
    for i = 1, cannonBalls.numChildren do
         print( i )
         if(tonumber(ballRemain.text) <= 0) then
             gameFun()
         end
    end
end

function gameFun()
    balloonText.isVisible = false
    balloonTextt.isVisible = false
    balloonTexttt.isVisible = false
    questionText.isVisible = false
    askUser = display.newText('Is the cannon hard to use?', display.contentCenterX, display.contentWidth / 4, native.systemFont, 20 ) 
    askUser:setFillColor(135, 75, 44)
    yesBtn = display.newImage("Yes.png",120,290)
    noBtn = display.newImage("No.png",190,290)
    yesBtn:addEventListener ('tap', ansQuestion)
    noBtn:addEventListener ('tap', ansQuestion)
end

function ansQuestion(event)
    if event.target==noBtn then
        answer = answer+0
    else
        answer = answer+1
    end
    print('send mail')
    sendMail()
    askUser.isVisible = false
    yesBtn.isVisible = false
    noBtn.isVisible = false
    yesBtn:removeEventListener ('tap', ansQuestion)
    noBtn:removeEventListener ('tap', ansQuestion)
    alert('lose')

end

function alert(state)
    gameListeners('rmv')

    local alert
    if(state == 'win') then
        alert = display.newImage('win.png')     
    else
        alert = display.newImage('lose.png')
    end
    askUser.isVisible = false
    yesBtn.isVisible = false
    noBtn.isVisible = false
    print("time from start: ", (system.getTimer()-gameTime))
    alert.anchorX = 0.5
    alert.anchorX = 0.5
    alert.x = display.contentCenterX
    alert.y = display.contentCenterY
    transition.from(alert, {time = 3000, xScale = 0.3, yScale = 0.3})

    local score = display.newText(score.text, 300, -30, native.systemFontBold, 20)
    score:setFillColor(135, 75, 44)
end

Also when sendMail function is on, it gives me this enter image description here

As far as i can tell, it keeps going back and forth between ansQuestion and alert(state). Is there a way to fix this?

No correct solution

OTHER TIPS

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.

In your alert function you passed the string value 'lose'. That's why it's always calling losing condition. As per my knowledge, you need to call this function.

local answer = 0

function ansQuestion(event)

if event.target==noBtn then

    answer = 0

else

    answer = 1

end

 sendMail()

askUser.isVisible = false
yesBtn.isVisible = false
noBtn.isVisible = false
yesBtn:removeEventListener ('tap', ansQuestion)
noBtn:removeEventListener ('tap', ansQuestion)
alert(answer)

end

function alert(state)

gameListeners('rmv')

local alert
if(state == 1) then
    alert = display.newImage('win.png')     
else
    alert = display.newImage('lose.png')
end
askUser.isVisible = false
yesBtn.isVisible = false
noBtn.isVisible = false
print("time from start: ", (system.getTimer()-gameTime))
alert.anchorX = 0.5
alert.anchorX = 0.5
alert.x = display.contentCenterX
alert.y = display.contentCenterY
transition.from(alert, {time = 3000, xScale = 0.3, yScale = 0.3})

local score = display.newText(score.text, 300, -30, native.systemFontBold, 20)
score:setFillColor(135, 75, 44)

end

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top