Lua - 重播按钮?

时间:2012-06-11 16:46:59

标签: lua corona

我正在使用Corona SDK制作应用。在游戏中你必须及时触及所有球体,如果你赢了,你就会出现一个胜利的图像。我在“你赢了”的消息下做了一个重播按钮,但我怎么说他重启整个游戏呢?

这是代码(不是全部):

function winLose(condition)
if condition == "Win" then
    print ("Winner")
    winMSG = display.newImage("media/win.png")


    -- Replay button

    local btnReplay = ui.newButton{
    default = "media/buttonWhite.png",
    over = "media/buttonWhiteOver.png",
    onPress = btnReplayPress,
    onRelease = btnReplayRelease,
    text = "Replay",
    textColor = { 0, 0, 0, 255 },
    size = 26,
    id = "replay",
    emboss = true
    }

    btnReplay.x = _W/2; 
    btnReplay.y = _H/2 + 50;

elseif condition == "Fail" then

    print ("Loser")
    loseMSG = display.newImage("media/lose.png")

    -- Replay Button

    btnReplay = ui.newButton{
    default = "media/buttonWhite.png",
    over = "media/buttonWhiteOver.png",
    onPress = btnReplayPress,
    onRelease = btnReplayRelease,
    textColor = { 0, 0, 0, 255 },
    size = 26,
    text = "Replay"
    id = "replay",
    emboss = true
    }


    -----

    function btnReplay:touch(e)
        if e.phase == "began" then
            --
        elseif e.phase == "moved" then
            --
        elseif e.phase == "ended" then
            -- Here I should say what it has to do to replay
        end
    end

    -----

    btnReplay:addEventListener( "touch", btnReplay);

    -----

    btnReplay.x = _W/2; 
    btnReplay.y = _H/2 + 50;
end
end

local function trackOrbs(obj)
obj:removeSelf()
o = o-1;

if time_up ~= true then
    if (o == 0) then
        timer.cancel(gametmr);
        winLose("Win");
    end
end
end 

local function countDown (e)
if (time_remain == total_secs) then
    ready = true
end
time_remain = time_remain - 1;
countdowntxt.text = time_remain;

if time_remain == 0 then
    time_up = true

    if o ~= 0 then
        winLose("Fail");
        ready = false;
    end
end
end


local function spawnOrb()

local orb = display.newImageRect("media/orb.png", 60, 60);
orb:setReferencePoint(display.CenterReferencePoint);
orb.x = mRand(50, _W-50); orb.y = mRand (50, _H-50);

function orb:touch(e)
    if time_up ~= true then
        if (ready == true) then
            if (e.phase == "ended") then
                trackOrbs(self);
            end 
        end 
    end

    return true
end

o = o + 1;

orb:addEventListener("touch", orb);

if (o == total_orbs) then
    gametmr =   timer.performWithDelay(1000, countDown, total_secs) ;
else
    ready = false
end
 end

 tmr = timer.performWithDelay (20, spawnOrb, total_orbs);

1 个答案:

答案 0 :(得分:0)

你可以像这样使用while循环

while true do 
    if mainGame then
        --game code
    elseif done then
        --buttons
        if button.restart then
            mainGame = true
        end
    end
 end