试图调用方法“插入”(一个零值)重装时的情景 [英] attempt to call method 'insert' (a nil value) when reloading a scene

查看:351
本文介绍了试图调用方法“插入”(一个零值)重装时的情景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,当我尝试重新加载场景。

I'm having some troubles when I try to reload a scene.

在我的计划中,用户都能够得出一个符合自己的手指(这是在iOS / Android应用程序),它的工作pretty不错,但是,当我尝试重新加载场景,控制台将返回

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

尝试索引字段'父'(一个零值)

"Attempt to index field 'parent' (a nil value)"

在78行

我来执行(在这种情况下,回放)的DrawLine和重新加载$ ​​C $ C特点是

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)

其中线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 ;)

推荐答案

您尝试到另一个场景不重置所有值,因为你将其设置为无,所以当你回到你的场景再次你的那一刻一个错误,我看到您的使用Director级来改变场景,去同一个场景你尝试使用故事板,我不知道这是否会更方便给你,因为我在想,你只是想重置价值在故事板可以重建和重置所有的值删除现场,去到另一个场景。您可以访问此链接比较主任级和故事板。
http://www.coronalabs.com/blog/2012/04/17/director-to-storyboard-transition-guide/

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. you can visit this link to compare director class and storyboard. http://www.coronalabs.com/blog/2012/04/17/director-to-storyboard-transition-guide/

这篇关于试图调用方法“插入”(一个零值)重装时的情景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆