当我尝试重新加载场景时,我遇到了一些麻烦。
在我的程序中,用户可以用他们的手指绘制一条线(它是iOS / Android应用程序),它工作得很好,但是,当我尝试重新加载场景时,控制台返回
“尝试索引字段'parent'(零值)”
第78行
我用来执行“drawline”和重新加载(在这种情况下重放)功能的代码是
local lines = {}
local lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = true
local i = 1
local function distanceBetween(x1, y1, x2, y2)
local dist_x = x2 - x1
local dist_y = y2 - y1
local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))
return distanceBetween
end
local function drawLine(e)
if(e.phase == "began") then
for i = #lines, 1, -1 do
if (lines[i]) then
if (lines[i].parent) then
lines[i].parent:remove(lines[i])
end
lines[i] = nil
end
end
lines = {}
line_number = 100
prevX = e.x
prevY = e.y
isDrawing = true
elseif(e.phase == "moved") then
local distance = distanceBetween(prevX, prevY, e.x, e.y)
if(isDrawing and distance < 100) then
if(lines[i]) then lineGroup:remove(i) end
lines[i] = display.newLine(prevX, prevY, e.x, e.y)
lines[i]:setColor(255, 255, 0)
lines[i].width = 3
lines[i].myName = "lines"
if(lines[i].y < 400) then
for i = #lines, 1, -1 do
if (lines[i]) then
if (lines[i].parent) then
lines[i].parent:remove(lines[i])
end
lines[i] = nil
end
end
end
local dist_x = e.x - prevX
local dist_y = e.y - prevY
physics.addBody(lines[i], "static",
{ density = 1, friction = 0.5, bounce =
-0.8, shape = {0, 0, dist_x, dist_y, 0, 0} } )
lineGroup:insert(lines[i])
end
elseif(e.phase == "ended") then
isDrawing = true
end
return lineGroup
end
Runtime:addEventListener("touch",drawLine)
第78行是:
lineGroup:insert(lines[i])
我使用
重启游戏local replayBTN = display.newImageRect("images/replay.png", 25, 25)
replayBTN.alpha = 1
replayBTN.x = _W/2
replayBTN.y = _H/2
localGroup:insert( replayBTN)
function replay(event)
director:changeScene("game")
return true
end
replayBTN:addEventListener("touch", replay)
如何解决问题?谢谢;)
答案 0 :(得分:0)
当您尝试转到另一个场景时,不要重置所有值,因为您将其设置为nil所以当您再次返回场景时会出现错误,我会看到您使用导演类更改场景如果您尝试使用故事板,请转到相同的场景我不知道它是否会对您更方便,因为我认为您只是想重置line
故事板的值可以删除场景并转到另一个场景时,重新创建并重置所有值。您可以访问此链接以比较导演课程和故事板。
http://www.coronalabs.com/blog/2012/04/17/director-to-storyboard-transition-guide/