什么是:“错误参数 #-1 到 'newLine'(代理预期,得到零)意味着

What does: "bad argument #-1 to 'newLine' (Proxy expected, got nil) mean

我一直在开发一款游戏。游戏涉及(一部分)获取触摸事件的 x 和 y 位置,并在它移动时跟踪它并将 x/y 值添加到 table。我对此很失望。但是,当我尝试从 table 创建一行时,它给了我这个错误:

/Users/jordanmcbride/Desktop/Programing/Lua Projects/When Balls Drop/main.lua:21: bad argument #-1 to 'newLine' (Proxy expected, got nil)
stack traceback:
    [C]: in function 'newLine'
    /Users/jordanmcbride/Desktop/Programing/Lua Projects/When Balls Drop/main.lua:21: in function </Users/jordanmcbride/Desktop/Programing/Lua Projects/When Balls Drop/main.lua:8>
    ?: in function <?:221>

我不知道那是做什么的...

有问题的代码,注释行。

function drawLineTouchListener( event )
    if event.phase == "began" then
        currx = event.x
        curry = event.y
        table.insert( linePositions, currx)
        table.insert( linePositions, curry)
    elseif event.phase == "moved" then
        currx = event.x
        curry = event.y
        table.insert( linePositions, currx)
        table.insert( linePositions, curry)
    elseif event.phase == "ended" then
        -- This is where the code does't work.
        line = display.newLine( linePositions )
        test = {
        0, 0,
        20, 20,
        40, 40, 
        60, 60, 
        80, 80}

        line2 = display.newLine( test)
        line2:setStrokeColor( black )
    end
    print( "x: "..currx.."  y:"..curry)
end

这是我的全部代码:

    -- Hides the status bar at the top of the screen
display.setStatusBar(display.HiddenStatusBar)

print("App Starting!")

linePositions = {}

function drawLineTouchListener( event )

    if event.phase == "began" then
        currx = event.x
        curry = event.y
        table.insert( linePositions, currx)
        table.insert( linePositions, curry)
    elseif event.phase == "moved" then
        currx = event.x
        curry = event.y
        table.insert( linePositions, currx)
        table.insert( linePositions, curry)
    elseif event.phase == "ended" then
        line = display.newLine( linePositions )
        test = {
        0, 0,
        20, 20,
        40, 40, 
        60, 60, 
        80, 80}

        line2 = display.newLine( test)
        line2:setStrokeColor( black )
    end


    print( "x: "..currx.."  y:"..curry)

end

function playButtonTouch ( event )

    if ( event.phase == "ended" ) then

    end
end

function highScoreButtonTouch ( event )

    if ( event.phase == "ended" ) then

    end
end

function quitButtonTouch ( event )

    if ( event.phase == "ended" ) then
        os.exit(0)
    end
end


function displayMenu ()
    mainMenuGroup = display.newGroup()

    title = display.newImage(mainMenuGroup, "Title.png", display.contentCenterX, 20) 
    title:scale( .3, .3 )

    playButton = display.newImage(mainMenuGroup, "Play Button.png", display.contentCenterX, display.contentHeight - 300)
    playButton:scale( .3, .3 )

    quitButton = display.newImage(mainMenuGroup, "Quit Button.png", display.contentCenterX, display.contentHeight - 200)
    quitButton:scale( .3, .3 )

    highScoreButton = display.newImage(mainMenuGroup, "Highscore Button.png", display.contentCenterX, display.contentHeight - 100)
    highScoreButton:scale( .3, .3 )

    playButton:addEventListener( "touch", playButtonTouch )

    quitButton:addEventListener( "touch", quitButtonTouch )

    highScoreButton:addEventListener( "touch", highScoreButtonTouch )
end


--Creates a solid white background that doubles for a touch listner
background = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight+100)
background:setFillColor(1, 1, 1 )

background:addEventListener( "touch", drawLineTouchListener )

displayMenu()

你的问题源于这样一个事实,即当函数需要单独的参数时,你传递了一个 table

相反,使用 unpack(linePositions) 将“解压”table 中的所有内容(通过将其作为元组返回)。

注意Lua 5.2 及更高版本中,它是table.unpack.