功能仅适用于失败状态而非获胜状态

时间:2014-03-02 20:19:29

标签: lua corona

当玩家输掉游戏时,会调用gameFun函数,但出于某种原因,当我将警报(win)切换到gameFun并且警报(丢失)在processAnswer中警告(win)时,它不起作用。我的目标是在赢得或输掉游戏后向用户提问。

--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

当sendMail功能打开时,它也会给我这个 enter image description here

据我所知,它在ansQuestion和alert(state)之间来回传递。有办法解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

当您在alert(lose)中致电ansQuestion()时,Lua会将lose解释为变量,而不是字符串。由于变量不存在,因此它为零,因此if (state == 'win')的第二个分支被执行。碰巧的是,在“失败”的情况下,这是你想要的分支,但它只是运气。当你执行alert(win)时,win也是一个变量,因此它是nil,因此执行相同的分支,那时它是错误的分支。使用

alert('lose')

alert('win')
正如你在ballCollision监听器中所做的那样。请注意,ansQuestion和alert中有几行重复的代码。

答案 1 :(得分:0)

在警报功能中,您传递了字符串值'lose'。这就是为什么它一直在呼唤失败状态。 据我所知,你需要调用这个函数。

本地答案= 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)

功能警报(状态)

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)