简体   繁体   English

尝试在重新加载场景时调用方法“插入”(nil值)

[英]attempt to call method 'insert' (a nil value) when reloading a scene

I'm having some troubles when I try to reload a scene. 尝试重新加载场景时遇到了一些麻烦。

In my program, users are able to draw a line with their finger (it's an iOS/Android app), and it works pretty good, but, when I try to reload the scene, the console returns 在我的程序中,用户可以用手指画一条线(这是一个iOS / Android应用程序),效果很好,但是当我尝试重新加载场景时,控制台会返回

"Attempt to index field 'parent' (a nil value)" “尝试索引字段“父级”(nil值)”

at line 78 在第78行

The code I've used to perform the "drawline" and the reload (replay in this case) features is 我用来执行“画线”和重新加载(在此情况下为重播)功能的代码是

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)

where line 78 is: 第78行是:

lineGroup:insert(lines[i])

I restart my game using 我使用重新启动游戏

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)

How can I fix my problem? 我该如何解决我的问题? THANKS ;) 谢谢 ;)

the moment you try to go to another scene don't reset all the values because you set it to nil so when you go back to your scene again you get an error and i see your using director class to change scene and go to the same scene have you try using storyboard i don't know if it will be much convenient to you because i'm thinking that your just trying to reset the value of the line storyboard can recreate and reset all the value as you remove the scene and go to another scene. 您尝试转到另一个场景的那一刻不要重置所有值,因为您将其设置为nil,所以当您再次返回到场景时,会出现错误,并且我看到您正在使用Director类更改场景并转到相同的位置您是否尝试过使用情节提要的场景我不知道这对您来说是否会很方便,因为我认为您尝试重置line情节提要的值可以在删除场景并继续时重新创建并重置所有值到另一个场景。 you can visit this link to compare director class and storyboard. 您可以访问此链接以比较导演班和故事板。 http://www.coronalabs.com/blog/2012/04/17/director-to-storyboard-transition-guide/ http://www.coronalabs.com/blog/2012/04/17/director-to-storyboard-transition-guide/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM