【问题标题】:lua managing alerts with timerlua 使用计时器管理警报
【发布时间】:2015-03-06 11:19:04
【问题描述】:

我是 LUA 的新手。必要时,我正在使用计时器弹出警报,由显示组组成。为了删除它们,我使用了 timeperformwithdelay 函数,该函数调用了一个删除函数,该函数删除了引用 Self 的显示组对象。 但是,我也有一个秒表可以使用计时器功能。当秒表为零时,我会弹出警报,2 秒后我想将其删除。但在这种情况下,删除功能不起作用。以下是代码:

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

【问题讨论】:

  • 你的remove函数是做什么的?它期望操作什么,因为我无法想象当以这种方式调用它时它会获得任何上下文或参数。
  • 嗨,Etan,感谢您的回答。其实这个功能没有任何作用!我想它应该删除我用来创建 screenAlert 的显示组(一个图像和两个文本行)。但是,例如,当我在游戏开始时调用它时,当我显示带有游戏级别编号的警报时,相同的函数会起作用。我错过了什么吗?
  • remove 函数是什么样的?需要争论吗?它是某个对象/表的“成员”函数吗?
  • Etan,这是我的代码:本地函数 timerDown() timeLimit = timeLimit-1 if timeLimit
  • 这是要删除的函数:local function removeAlert() alertDisplayGroup:removeSelf() end

标签: lua coronasdk


【解决方案1】:

我修改了您的代码以进行测试。到目前为止,我制作的这段代码可以满足您想要的要求。对于在 lua 中工作的函数,该函数必须位于该函数的顶部,这意味着如果您想从函数 B 调用函数 A。函数 A 必须位于该函数的顶部 函数 B.

示例:

local functionA ()

end

local functionB ()

  functionA()

end

这是我为使您的代码按您的喜好工作而编写的代码 sn-p:

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)

如您所见。我对其进行了一些编辑以进行测试。干杯。

【讨论】:

  • 非常感谢!它工作正常! :) 我花了很多时间为此苦苦挣扎!
  • 没有问题我的朋友! :) 快乐编码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
相关资源
最近更新 更多