2013-07-02 78 views
1

我在嘗試重新加載場景時遇到了一些麻煩。嘗試在重新加載場景時調用方法「插入」(一個零值)

在我的計劃中,用戶都能夠得出一個符合自己的手指(這是在iOS/Android應用程序),和它的作品不錯,但,當我嘗試重新加載場景,控制檯返回

「嘗試索引字段‘父’(一個零值)」

在管線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

你得到了一個完整的堆棧跟蹤錯誤? – greatwolf

回答

0

當你嘗試去另一個場景的時候不重置所有的值,因爲你將它設置爲零,所以當你再次回到你的場景時,你會得到一個錯誤,我看到你的導演類改變場景,並去同一場景你嘗試使用故事板,我不知道它是否會很方便,因爲我想你只是試圖重置line故事板的值可以重新創建和重置所有的價值當你移除場景並轉到另一場景時。你可以訪問這個鏈接來比較導演類和故事板。 http://www.coronalabs.com/blog/2012/04/17/director-to-storyboard-transition-guide/

+0

非常感謝!我已經嘗試使用storyboard而不是導演,但是我得到了同樣的錯誤。順便說一句,我會看看剛剛發佈的指南;) – luaLover

相關問題