1

シーンをリロードしようとすると問題が発生します。

私のプログラムでは、ユーザーは指で線を引くことができ (これは iOS/Android アプリです)、かなりうまく機能しますが、シーンをリロードしようとすると、コンソールが返されます

「フィールド 'parent' (nil 値) のインデックスを作成しようとしています」

39行目

「ドローライン」とリロード (この場合はリプレイ) 機能を実行するために使用したコードは次のとおりです。

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
   lines[i].parent:remove(lines[i])
   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
   lines[i].parent:remove(lines[i])
   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)

39行目はどこですかlines[i].parent:remove(lines[i])

助言がありますか?ありがとう!:)

** *ゲームを再起動する方法は次のとおりです* ** * ** * ** * **

--REPLAY

 local replayBTN = display.newImage("immagini/pause.png")

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

1 に答える 1

0

必要に応じて、追加のチェックを行うことができます。

  if (lines[i]) then
      if (lines[i].parent) then
         lines[i].parent:remove(lines[i])
      end
      lines[i] = nil
  end
于 2013-06-22T11:53:57.487 に答える