簡體   English   中英

嘗試在重新加載場景時調用方法“插入”(nil值)

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

嘗試重新加載場景時遇到了一些麻煩。

在我的程序中,用戶可以用手指畫一條線(這是一個iOS / Android應用程序),效果很好,但是當我嘗試重新加載場景時,控制台會返回

“嘗試索引字段“父級”(nil值)”

在第78行

我用來執行“畫線”和重新加載(在此情況下為重播)功能的代碼是

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)

我該如何解決我的問題? 謝謝 ;)

您嘗試轉到另一個場景的那一刻不要重置所有值,因為您將其設置為nil,所以當您再次返回到場景時,會出現錯誤,並且我看到您正在使用Director類更改場景並轉到相同的位置您是否嘗試過使用情節提要的場景我不知道這對您來說是否會很方便,因為我認為您嘗試重置line情節提要的值可以在刪除場景並繼續時重新創建並重置所有值到另一個場景。 您可以訪問此鏈接以比較導演班和故事板。 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