Memory 尝试在lua/sdk中索引nil变量时出现问题
我是lua的新手,正如您可能从代码中了解到的,我正在尝试通过在按钮对象上使用事件侦听器来删除一个在时间结束时显示“停止”的对象,该按钮对象也是在时间结束时创建的。这将返回尝试索引全局“stopit”(一个nil值)的错误。我在将其添加到屏幕的类中将其声明为一个局部变量,因此我不确定发生了什么。我已经组织并尝试了几种不同的方法,我无法让它不断循环而不立即或在一、两轮比赛后随机崩溃 代码如下:Memory 尝试在lua/sdk中索引nil变量时出现问题,memory,lua,coronasdk,null,Memory,Lua,Coronasdk,Null,我是lua的新手,正如您可能从代码中了解到的,我正在尝试通过在按钮对象上使用事件侦听器来删除一个在时间结束时显示“停止”的对象,该按钮对象也是在时间结束时创建的。这将返回尝试索引全局“stopit”(一个nil值)的错误。我在将其添加到屏幕的类中将其声明为一个局部变量,因此我不确定发生了什么。我已经组织并尝试了几种不同的方法,我无法让它不断循环而不立即或在一、两轮比赛后随机崩溃 代码如下: display.setStatusBar(display.HiddenStatusBar) local
display.setStatusBar(display.HiddenStatusBar)
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local score = 0
local dextime;
local stopit;
local button3;
function newTarget(event)
timer.performWithDelay(100, function() display.remove(target) end)
transition.to(target, {time=99, xScale=.4, yScale=.4})
timer.performWithDelay(101, dexit)
score = score + 10
scoreTxt.text = ("Score:" .. score)
end
function dexit()
stopit = display.newImage("stop.png")
stopit.x = 300
stopit.y = 600
stopit.isVisible = false
button3 = display.newImage("button3.png")
button3:addEventListener("tap", removeitems)
button3.x = centerX
button3.y = centerY
button3.isVisible = false
target = display.newImage("target.png")
target.xScale = .25
target.yScale = .25
target.x = math.random(50, 550)
target.y = math.random(50, 750)
target:addEventListener("tap", newTarget)
local function removeitems(event)
stopit:removeSelf()
button3:removeSelf()
scoreTxt:removeSelf()
timerTxt:removeSelf()
timer.performWithDelay(500, setup)
dextime = 15
score = 0
end
timer.performWithDelay(15000, function() display.remove(target) end)
timer.performWithDelay( 15000, function() button3.isVisible = true end)
timer.performWithDelay(15000, function() stopit.isVisible = true end)
end
local function dexgo()
timer.performWithDelay(1000, function() dextime = dextime - 1 end, 15)
timer.performWithDelay(1001, function() timerTxt.text = ("Time:" .. dextime) end, 15)
dexit()
end
local function one2()
local one = display.newImage("1.png")
one.x = centerX
one.y = centerY
one.alpha = 0
transition.to(one, {time=1000, alpha =1, onComplete=dexgo})
timer.performWithDelay( 1000, function()
display.remove(one)
end, 1)
end
local function two2()
local two = display.newImage("2.png")
two.x = centerX
two.y = centerY
two.alpha = 0
transition.to(two, {time=1000, alpha =1, onComplete=one2})
timer.performWithDelay( 1000, function()
display.remove(two)
end, 1)
end
local function dexMode()
local three = display.newImage("3.png")
three.x = centerX
three.y = centerY
three.alpha = 0
timerTxt = display.newText("Time:" .. dextime,-1, centerX - 440, "Helvetica", 40)
scoreTxt = display.newText( "Score:" .. score, 440, -140, "Helvetica", 40)
display.remove(mode1)
display.remove(mode2)
display.remove(title)
transition.to(three, {time=1000, alpha =1, onComplete=two2})
timer.performWithDelay( 1000, function()
display.remove(three)
end, 1)
bg = nil
title = nil
mode1 = nil
mode2 = nil
end
function listener(event)
simpleMode()
end
function listener2(event)
dexMode()
end
function startGame()
transition.to( title, { time=2000, y=0, alpha=.9, onComplete=showTitle})
transition.to(bg, { time=2000, y=centerY, alpha=1})
transition.to(mode1, { time=2000, x=centerX, alpha=.9})
transition.to(mode2, { time=2000, x=centerX, alpha=.9})
end
function setup()
dextime = 15;
bg = display.newImage("background.png")
bg.yScale = 1.4
bg.alpha = 0
title = display.newImage("title.png")
title.x = centerX
title.y = -200
title.alpha = 0
mode1 = display.newImage("button1.png")
mode1.xScale = 1.23
mode1.yScale = 1.23
mode1.x = 800
mode1.y = 500
mode1.alpha = 0
mode2 = display.newImage("button2.png")
mode2.xScale = 1.23
mode2.yScale = 1.23
mode2.x = -200
mode2.y = 625
mode2.alpha = 0
mode1:addEventListener( "touch", listener )
mode2:addEventListener( "touch", listener2 )
startGame()
end
setup()
函数删除项(事件)
中的instopit
按钮3
、scoreTxt
和timerTxt
的使用是全局范围的。在dexit
中调用removietems
时,它无法看到您在dexit
中声明的局部变量
最简单的解决方案是将removietems
移动到dexit
中,使其成为闭包:
function dexit()
local stopit = display.newImage("stop.png")
local button3 = display.newImage("button3.png")
local function removeitems(event)
stopit:removeSelf()
button3:removeSelf()
scoreTxt:removeSelf()
timerTxt:removeSelf()
timer.performWithDelay(500, setup)
end
-- ...
end
试试这个:
if(stopit~=nil)then
stopit:removeSelf()
end
由于计时器和转换,您将收到此错误。当您在不取消计时器或转换的情况下删除对象时,它会在循环中调用,并在删除一次后获得nil值。 首先,在停止游戏或改变场景时,你需要将所有的定时器和转换存储在数组中,并释放数组。然后重新初始化数组 例如:本地timerId={} 局部TransitionID={} timerId[#timerId+1]=timer.performWithDelay(15000,function()display.remove(target)end) TransitionID[#TransitionID+1]=transition.to(2,{time=1000,alpha=1,onComplete=one2}) 移除所有对象时,首先移除计时器和转换 对于i=1,#timerId do
timer.cancel(timerId[i])
timerId[i] = nil
timerId = {} //Initializing array
end
对于j=1,#transitionID do
transition.cancel(transitionID[j])
transitionID[j] = nil
transitionID = {}
结束我更新了代码以反映您建议的更改,但它仍然给了我一个我不理解的错误,还有其他理论吗?您可以添加完整的错误消息吗?它应该说明这发生在脚本中的哪一行。在定义
removitems
之前,您正在执行addEventListener
。