lua使用计时器管理警报

lua使用计时器管理警报,lua,coronasdk,Lua,Coronasdk,我在卢阿还是个新手。我使用计时器弹出警报,必要时由displaygroup组成。要删除它们,我使用timeperformwithdelay函数调用一个删除函数,该函数删除使用Self引用的displaygroup对象。 然而,我有一个秒表与定时器功能以及工作。当秒表归零时,我弹出警报,2秒钟后我想将其删除。但是在这种情况下,remove函数不起作用。代码如下: if timerleft==0 then screenalert("Game 1","Time is over") perf

我在卢阿还是个新手。我使用计时器弹出警报,必要时由displaygroup组成。要删除它们,我使用timeperformwithdelay函数调用一个删除函数,该函数删除使用Self引用的displaygroup对象。 然而,我有一个秒表与定时器功能以及工作。当秒表归零时,我弹出警报,2秒钟后我想将其删除。但是在这种情况下,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
local函数timerDown()
时间限制=时间限制-1
如果时间限制小于10,则
timeLeft.text=“0:0”。.timeLimit
其他的
timeLeft.text=“0:”…时间限制
终止
如果(timeLimit==0),则
timeLeft.text=“0:00”
alertScreen(“游戏1”,“时间结束!”)
定时器。延时性能(2000,removeAlert,1)
时限=60
终止
终止
局部函数removeAlert()
alertDisplayGroup:removeSelf()
终止
功能警报屏幕(标题、消息)
alertBox=display.newImage(“cornice.png”)
alertBox.x=W
alertBox.y=H/1.3
titolomessaggio=display.newText(标题,0,0,“Arial”,200)
titolomessaggio:setTextColor(255255,0255)
titolomessaggio.xScale=0.5
titolomessaggio.yScale=0.5
titolomessaggio.x=display.contentCenterX
titolomessaggio.y=display.contentCenterY-200
testomessaggio=display.newText(消息,0,0,“Arial”,100)
testomessaggio:setTextColor(255255,0255)
testomessaggio.xScale=0.5
testomessaggio.yScale=0.5
testomessaggio.x=display.contentCenterX
testomessaggio.y=display.contentCenterY+10
alertDisplayGroup=display.newGroup()
alertDisplayGroup:插入(alertBox)
alertDisplayGroup:插入(titolomessaggio)
alertDisplayGroup:插入(testomessaggio)
终止

我修改了您的代码以测试它。到目前为止,我编写的这段代码能够满足您的需求。要使函数在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)
localalertdisplaygroup=display.newGroup()
本地时间限制=5
局部函数removeAlert()
alertDisplayGroup:removeSelf()
终止
本地函数timerDown()
时间限制=时间限制-1
打印(时间限制)
--如果时间限制小于10,则
--打印(时间限制)
--否则
--打印(时间限制)
--结束
如果(timeLimit==0),则
--timeLeft.text=“0:00”
alertScreen(“游戏1”,“时间结束!”)
定时器。延时性能(2000,removeAlert,1)
时限=5
终止
终止
功能警报屏幕(标题、消息)
alertBox=display.newImageRect(“images/error_message.png”,25285)
alertBox.x=屏幕W/2
alertBox.y=屏幕H/2
titolomessaggio=display.newText(标题,0,0,“Arial”,200)
titolomessaggio:setTextColor(255255,0255)
titolomessaggio.xScale=0.5
titolomessaggio.yScale=0.5
titolomessaggio.x=display.contentCenterX
titolomessaggio.y=display.contentCenterY-200
testomessaggio=display.newText(消息,0,0,“Arial”,100)
testomessaggio:setTextColor(255255,0255)
testomessaggio.xScale=0.5
testomessaggio.yScale=0.5
testomessaggio.x=display.contentCenterX
testomessaggio.y=display.contentCenterY+10
alertDisplayGroup=display.newGroup()
alertDisplayGroup:插入(alertBox)
alertDisplayGroup:插入(titolomessaggio)
alertDisplayGroup:插入(testomessaggio)
终止
timer.performWithDelay(1000,函数()timerDown()结束,0)

如你所见。我对它进行了一点编辑以测试它。干杯。

您的
删除功能做什么?因为我无法想象它会得到任何上下文或参数,所以它期望在什么情况下运行。嗨,伊坦,谢谢你的回答。其实这个功能没有任何效果!我想它应该删除我用来创建screenAlert的displaygroup(一个图像和两个文本行)。但同样的功能也可以使用,例如,当我在游戏开始时调用它时,当我显示带有游戏级别编号的警报时。我遗漏了什么吗?
remove
函数是什么样子的?需要争论吗?它是某个对象/表的“成员”函数吗?Etan,这是我的代码:局部函数timerDown()timeLimit=timeLimit-1如果timeLimit<10,那么timeLeft.text=“0:0”。timeLimit else timeLeft.text=“0:…timeLimit end if(timeLimit==0)然后timeLeft.text=“0:00”alertScreen(“游戏1”,“时间结束!”)计时器。performWithDelay(2000,removeAlert,1)timeLimit=60 end,这是要删除的函数:本地函数removeAlert()alertDisplayGroup:removeSelf()谢谢你,再见!它很好用!:)我为此花了很多时间!没问题,我的朋友!:)快乐编码