我一直在做游戏。游戏涉及(一部分)获取触摸事件的x和y位置,并在移动时跟踪它并将x / y值添加到表中。我有点压力。但是,当我尝试从表中创建一行时,它会给我这个错误:
/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()
答案 0 :(得分:0)
您的问题源于您在函数需要单个参数时传递表的事实。
相反,请使用{&#34; 解包&#34;表中的所有内容(通过将其作为元组返回)。
注意在 Lua 5.2 及更高版本中,它是table.unpack。