Timer Corona SDK-从按键处理程序上的小部件Candy按钮中取消计时器
我正在尝试取消小部件Candy按钮的onPress事件处理程序中的计时器。但是,timerId始终为nil,即使我已将其定义为文件范围内的局部变量。我是Lua开发的新手,因此我假设这个问题与变量范围有关,但我很难找到一种方法来访问计时器变量,而不使其成为真正的全局变量(即声明它时不使用“local”关键字)。请参阅下面的代码片段Timer Corona SDK-从按键处理程序上的小部件Candy按钮中取消计时器,timer,event-handling,lua,coronasdk,Timer,Event Handling,Lua,Coronasdk,我正在尝试取消小部件Candy按钮的onPress事件处理程序中的计时器。但是,timerId始终为nil,即使我已将其定义为文件范围内的局部变量。我是Lua开发的新手,因此我假设这个问题与变量范围有关,但我很难找到一种方法来访问计时器变量,而不使其成为真正的全局变量(即声明它时不使用“local”关键字)。请参阅下面的代码片段 local countdownTimer = timer.performWithDelay(1000, handleCountdownTimer, countdown)
local countdownTimer = timer.performWithDelay(1000, handleCountdownTimer, countdown);
local answerButton1 = wcandy.NewButton
{
x = "center",
y = "55%",
width = "75%",
name = "MyButton1",
theme = "theme_1",
border = {"normal",6,1, .12,.12,0,.4, .72,.72,.72,.6},
pressColor = {1,1,1,.25},
caption = "Touch me!",
textAlign = "center",
fontSize = "40",
onPress = function( EventData )
timer.cancel(countdownTimer); -- "countdownTimer" is always nil!!!
local button = wcandy.GetHandle("MyButton1");
if button:get("caption") == tostring(solution) then
questionText:set("caption", "Correct!");
else
questionText:set("caption", "Wrong!");
end
end
}
尝试这样取消:
if(countdownTimer~=nil) -- Check whether timer is triggering or not
timer.cancel(countdownTimer)
end
我看你发布的代码没有错:当onPress被调用时,你的倒计时应该是非零的。您不必测试倒计时是否为零。在创建新按钮之前,请在创建后打印倒计时,以确认它不是零。同时搜索代码以查找变量引用的所有位置,并检查是否有任何位置将其设置为nil 更新: 您可以尝试移动函数并使其成为本地函数:
local countdownTimer = timer.performWithDelay(1000, handleCountdownTimer, countdown);
local function cancelTimer( EventData )
timer.cancel(countdownTimer); -- "countdownTimer" is always nil!!!
local button = wcandy.GetHandle("MyButton1");
if button:get("caption") == tostring(solution) then
questionText:set("caption", "Correct!");
else
questionText:set("caption", "Wrong!");
end
end
local answerButton1 = wcandy.NewButton
{
x = "center",
y = "55%",
width = "75%",
name = "MyButton1",
theme = "theme_1",
border = {"normal",6,1, .12,.12,0,.4, .72,.72,.72,.6},
pressColor = {1,1,1,.25},
caption = "Touch me!",
textAlign = "center",
fontSize = "40",
onPress = cancelTimer
}
更新2(在@husterk发现上述方法仍然无效后):
我看不出有什么问题,所以我已经在我的电晕sim卡上测试过了,没有问题,所以问题出在别处。以下是main.lua的全部内容:
local widget = require('widget')
local function handleCountdownTimer(event)
print('timed out; timer:', event.source)
end
local countdownTimer
local function cancelTimer( event )
print('cancelling timout, timer:', countdownTimer)
timer.cancel(countdownTimer)
end
local answerButton1 = widget.newButton
{
onPress = cancelTimer,
width = 100,
height = 100,
label = 'hello',
emboss = true
}
countdownTimer = timer.performWithDelay(10000, handleCountdownTimer)
print('timer started:', countdownTimer)
我刚刚想到的一件事:是什么让你在取消行说“countdownTimer总是nil!!!”如果是错误消息,你可以打印它,或者在计时器前有一个print语句。取消?@Scholli-我正在使用Lua Glider IDE,我可以用调试器检查变量。变量显示为nil,并且在我尝试执行它时也报告以下错误:?:0:尝试索引nil值消息堆栈回溯:?:在函数中,感谢您的建议,但onPress处理程序中的倒计时肯定为nil。但是,如果我在主文件作用域中执行相同的timer.cancel(countdownTimer)函数,一切正常。事件处理程序似乎在不同的范围内运行(有点像.NET中在UI线程之外的单独线程上运行的事件)。更新会导致相同的问题。我开始怀疑这个问题是否是由小部件Candy button对象引起的。我还要测试一个标准按钮。我会让你知道这是否解决了问题。我刚刚测试了从标准按钮事件处理程序取消计时器,同样的问题也发生了。我不知所措……我举了一个有效的例子。您显示的代码没有问题,因此问题出在其他地方,您必须在其他地方删除它。如我在回答中所述,搜索所有事件。另外,请参阅更新。这很奇怪。因此,如果您在使用timer.performWithDelay函数实际设置值之前声明countdownTimer局部变量,那么一切都正常。但是,如果将变量指定在与timer.performWithDelay函数相同的行上,则该变量将中断。这有什么原因吗?谢谢你的建议,但这没有帮助。我需要能够在按下按钮后立即停止计时器。我不能跳过停止计时器,因为我可以看到它仍在运行,即使onPress事件处理程序中的countdownTimer引用显示为nil。请尝试此本地tm local number=0函数fn_counter()number=number+1 txt_counter.text=number如果number>=10,则使用timer.cancel(tm)end end tm=定时器性能和延迟(1000,fn_计数器,0)