使用定时器管理警报的 Lua。

我在 LUA 中还比较新。当需要时,我使用计时器来弹出由 displaygroup 组成的警报。为了移除它们,我使用 timeperformwithdelay 函数调用一个 remove 函数,该函数会将 Self 引用的 displaygroup 对象移除。但是,我还有一个使用计时器函数的秒表。当秒表归零时,我弹出警报,然后在两秒钟后想要将其移除。但是,在这种情况下,remove 函数不起作用。以下是代码:

if timerleft==0 then
   screenalert("Game 1","Time is over")
   performwithdelay(2000,remove,1)

end

谢谢 嗨,再次感谢您的支持。这是我的代码:

local function timerDown()
    timeLimit = timeLimit - 1
    if timeLimit < 10 then
        timeLeft.text = "0:0"..timeLimit
    else
        timeLeft.text = "0:"..timeLimit
    end
    
    if timeLimit == 0 then
        timeLeft.text="0:00"
        alertScreen("Game 1","Time is over!")
        timer.performWithDelay(2000,removeAlert,1)
        timeLimit = 60
    end
end

local function removeAlert()
    alertDisplayGroup:removeSelf()
end

function alertScreen(title, message)

    alertBox = display.newImage("cornice.png")
    alertBox.x = W
    alertBox.y = H/1.3
    
    titolomessaggio = display.newText(title,0,0,"Arial",200)
    titolomessaggio:setTextColor(255,255,0,255)
    titolomessaggio.xScale = 0.5
    titolomessaggio.yScale = 0.5
    titolomessaggio.x = display.contentCenterX
    titolomessaggio.y = display.contentCenterY-200
    
    testomessaggio = display.newText(message,0,0,"Arial",100)
    testomessaggio:setTextColor(255,255,0,255)
    testomessaggio.xScale = 0.5
    testomessaggio.yScale = 0.5
    testomessaggio.x = display.contentCenterX
    testomessaggio.y = display.contentCenterY+10
    
    alertDisplayGroup = display.newGroup()
    alertDisplayGroup:insert(alertBox)
    alertDisplayGroup:insert(titolomessaggio)
    alertDisplayGroup:insert(testomessaggio)
end
点赞
用户3739502
用户3739502

我修改了你的代码来进行测试。到目前为止,我制作的这个代码与你想要的要求配合良好。在lua中,要使函数有效,该函数必须位于该函数的顶部,这意味着如果您想从函数B调用函数A,函数A必须在函数B的顶部。

例如:

local functionA ()

end

local functionB ()

  functionA()

end

这里是我制作的代码片段,可以使您的代码按照您的意愿工作:

local alertDisplayGroup = display.newGroup()
local timeLimit = 5

local function removeAlert()
   alertDisplayGroup:removeSelf()
end

local function timerDown()
   timeLimit = timeLimit-1
    print(timeLimit)
   -- if timeLimit < 10 then
   --  print(timeLimit)

   -- else
   --  print(timeLimit)
   -- end

 if(timeLimit==0)then
    --timeLeft.text="0:00"
    alertScreen("Game 1","Time is over!")

    timer.performWithDelay(2000,removeAlert,1)
    timeLimit=5
 end
end

function alertScreen(title, message)

alertBox=display.newImageRect("images/error_message.png", 252, 85)
alertBox.x=screenW/2
alertBox.y=screenH/2
titolomessaggio=display.newText(title,0,0,"Arial",200)
titolomessaggio:setTextColor(255,255,0,255)

titolomessaggio.xScale=0.5
titolomessaggio.yScale=0.5

titolomessaggio.x=display.contentCenterX
titolomessaggio.y=display.contentCenterY-200

testomessaggio=display.newText(message,0,0,"Arial",100)
testomessaggio:setTextColor(255,255,0,255)
testomessaggio.xScale=0.5
testomessaggio.yScale=0.5

testomessaggio.x=display.contentCenterX
testomessaggio.y=display.contentCenterY+10

alertDisplayGroup=display.newGroup()
alertDisplayGroup:insert(alertBox)
alertDisplayGroup:insert(titolomessaggio)
alertDisplayGroup:insert(testomessaggio)
end

timer.performWithDelay( 1000, function() timerDown() end, 0)

正如您所看到的。我稍微编辑了它以进行测试。干杯。

2015-03-06 03:19:04