提示用户在电晕游戏中间回答某些问题

时间:2014-03-01 19:17:10

标签: lua corona

有没有办法在游戏过程中或用户赢/输游戏后向用户询问问题?下面的代码是我写的代码,要求玩家同意,我稍微改了一下。我不知道如何更改它,而且我想将回复作为电子邮件发送给自己。

--Asking user a question, response is printed out in the Simulator Output and emailed to me
local alert = native.showAlert( "Question", "Are the controls difficult to master?", { "Yes", "No" }, gameFun )
local function gameFun( event )
    print( "User's decision is ".. event.index .. "    User answered the question " .. event.action )

    local action = event.action
    if "clicked" == event.action then
        if 2 == event.index then

        end
    elseif "cancelled" == event.action then
        -- our cancelAlert timer function dismissed the alert so do nothing
    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? Rate it at a scale of 1-5(5 the best)', 
        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)
end

function gameFunListener(action)
    if (action=='add') then
        yesBtn:addEventListener ('tap', sendMail())
        noBtn:addEventListener ('tap', sendMail())
    else
        yesBtn:removeEventListener ('tap', sendMail())
        noBtn:removeEventListener ('tap', sendMail())
    end
end

我创建了一个新功能,它会询问用户一个问题,如果用户按是或否,他们会转到电子邮件选项,他们可以发送电子邮件告诉我。但由于某种原因,按钮不起作用,它跳转到写一封电子邮件。

1 个答案:

答案 0 :(得分:0)

游戏中的某个位置(如enterFrame事件处理程序或其他事件处理程序)您的游戏确定用户已赢或输。这是您调用askQuestion()(或调查)函数的地方。有很多方法可以做到这一点,但基本上是

  • 如果您使用的是故事板,则可以将问题转换为您转换到的新场景(currentScene.gotoScene("newscene"));
  • 否则(没有故事板)你可以使一些可见的显示对象不可见,实例化一些问题小部件(问题文本,答案按钮等)并使那些可见

当上一个问题得到解答时,您可以将视图恢复到问题之前的状态(或者显示"游戏结束"页面等)。 Corona,作为其他GUI,基于用户操作产生的事件(按下按钮,输入文本,移动对象与另一个碰撞等)和系统事件(enterFrame,orientation,suspend / resume等),因此导致显示问题的触发器位于其中一个事件处理程序中。

例如:

function gameFun()
    balloonText.isVisible = false
    ...
    askUser = display.newText(
        'Is the cannon hard to use? Rate it at a scale of 1-5(5 the best)', 
        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', processAnswer)
    noBtn:addEventListener ('tap', processAnswer)
end

function processAnswer(event)
    if event.target == noBtn then 
        ...
    else
        ...
    end
    sendMail()
    askUser.isVisible = false
    yesBtn.isVisible = false
    ...
    yesBtn:removeEventListener ('tap', processAnswer)
    noBtn:removeEventListener ('tap', processAnswer)
end

请注意,与注册监听器时相比,不得调用正在注册的函数(函数名后面没有())!此外,如果您在开始时创建问题显示对象,然后在gameFun中切换其可见性并在processAnswer中关闭,那么您不必担心removeEventListener:它们仅在可见时响应tap。