0

我对 lua 很陌生,正如您可能从代码中看出的那样,我正在尝试删除一个显示“停止!”的对象。当时间用完时,在按钮对象上使用事件侦听器,该对象也是在时间用完时创建的。这将返回错误尝试索引全局“stopit”(一个 nil 值)。我在将它添加到屏幕的类中将它声明为本地变量,所以我不确定发生了什么。我已经组织并尝试了几种不同的方法,但我无法让它持续循环,而不会立即或在一两轮游戏之后随机崩溃。

这是代码:

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()
4

3 回答 3

1

in和instopit button3的使用 是全局范围的。当被调用时,它看不到你在.scoreTxttimerTxtfunction removeitems(event)removeitemsdexitdexit

最简单的解决方案是removeitems通过将其移动到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
于 2013-06-10T21:52:21.137 回答
0

由于计时器和转换,您会收到此错误。当您在不取消计时器或转换的情况下删除对象时,它会在循环中调用并在删除一次后获取 nil 值。首先,您需要在停止游戏或更改场景时将所有计时器和转换存储在数组和释放数组中。然后再次重新初始化数组。

例如:本地 timerId = {}

本地转换 ID = {}

timerId[#timerId+1] = timer.performWithDelay(15000, function() display.remove(target) end)

TransitionID[#TransitionID+1] = transition.to(二, {time=1000, alpha =1, onComplete=one2})

删除所有对象时,首先删除计时器和过渡。

对于 i = 1,#timerId 做

        timer.cancel(timerId[i])
        timerId[i] = nil
        timerId = {} //Initializing array
end

对于 j = 1,#transitionID 做

 transition.cancel(transitionID[j])
 transitionID[j] = nil
  transitionID = {}

结尾

于 2014-03-20T10:01:51.043 回答
0

尝试这个:

if(stopit~=nil)then
    stopit:removeSelf()
end
于 2013-06-11T04:13:15.537 回答