CORONA:timer.cancel()返回“尝试索引空值". [英] CORONA: timer.cancel() returns "Attempt to index a nil value"
问题描述
我正试图取消在另一个触摸事件"功能内的触摸事件"功能中启动的计时器,如下所示:
I'm trying to cancel a timer started in a "touch event" function inside another "touch event" function, as shown below:
local function startNewGame(event)
if(event.phase=="ended")then
local function animationImmaginiOggetti()
for i=1, 7 do
transition.to(immaginiOggettiAvvioPartita[i],
{ time = 200, delay = 0, xScale = 0, yScale = 0, alpha = 0})
end
end
local function removeImmaginiOggetti()
if immaginiOggettiAvvioPartita[1] then
for i=1, 11 do
immaginiOggettiAvvioPartita[i]:removeSelf()
immaginiOggettiAvvioPartita[i] = nil
end
end
end
local tmrAIO = timer.performWithDelay(4000, animationImmaginiOggetti, 1)
local tmrRIO = timer.performWithDelay(4250, removeImmaginiOggetti, 1)
end
end
local function replayGame(event)
if(event.phase=="ended")then
timer.cancel(tmrAIO)
timer.cancel(tmrRIO)
end
end
startBTN:addEventListener("touch", startNewGame)
replayBTN:addEventListener("touch", replayGame)
我的问题是电晕回来了
文件:?
Attempt to index a nil value" on timer.cancel (tmrAIO)
.
我在做什么错了?
推荐答案
问题如下,变量tmrAIO
和tmrRIO
对于函数startNewGame
是局部的,这意味着只能从函数中访问它们. startNewGame
定义的作用域,现在您正尝试从该函数外部访问这两者,并且它们未在该作用域中定义,这就是nil
值的原因.
The problem is the following, the variables tmrAIO
and tmrRIO
are local to the function startNewGame
, that means they can only be accessed from the scope defined by startNewGame
and right now you are trying to access both from outside that function and they are not defined in that scope that's why the nil
value.
解决方案:
local tmrAIO
local tmrRIO
local function startNewGame(event)
if(event.phase=="ended")then
local function animationImmaginiOggetti()
for i=1, 7 do
transition.to(immaginiOggettiAvvioPartita[i],
{time = 200, delay = 0, xScale = 0, yScale = 0, alpha = 0})
end
end
local function removeImmaginiOggetti()
if immaginiOggettiAvvioPartita[1] then
for i=1, 11 do
immaginiOggettiAvvioPartita[i]:removeSelf()
immaginiOggettiAvvioPartita[i] = nil
end
end
end
tmrAIO = timer.performWithDelay(4000, animationImmaginiOggetti, 1)
tmrRIO = timer.performWithDelay(4250, removeImmaginiOggetti, 1)
end
end
local function replayGame(event)
if(event.phase=="ended")then
timer.cancel(tmrAIO)
timer.cancel(tmrRIO)
end
end
startBTN:addEventListener("touch", startNewGame)
replayBTN:addEventListener("touch", replayGame)
如您所见,我在startNewGame
范围之外声明了tmrAIO
和tmrRIO
,使它们在文件内的任何位置都可以访问.
As you can see I declared tmrAIO
and tmrRIO
outside of the scope of startNewGame
making them accessible anywhere inside this file.
这篇关于CORONA:timer.cancel()返回“尝试索引空值".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!