Lua 在线创建时出现错误
我不明白为什么会出现以下错误: 线路:107 错误参数#1到“换行符”(应为数字,为零) 我试图在两个被触摸的物体之间创建一条线 这是我的密码:Lua 在线创建时出现错误,lua,newline,coronasdk,null,Lua,Newline,Coronasdk,Null,我不明白为什么会出现以下错误: 线路:107 错误参数#1到“换行符”(应为数字,为零) 我试图在两个被触摸的物体之间创建一条线 这是我的密码: function createstar() ie = ie - 300 astar = display.newImage('ls.png', math.random( 1, 10) * 33, ie) astar:addEventListener( "touch", star) physics.addBody(asta
function createstar()
ie = ie - 300
astar = display.newImage('ls.png', math.random( 1, 10) * 33, ie)
astar:addEventListener( "touch", star)
physics.addBody(astar)
stars:insert(astar)
sceneGroup:insert(stars)
end
function update(e)
if(stars ~= nil)then
for i = 1, stars.numChildren do
stars[i].y = stars[i].y + 3
end
end
end
function star:touch( event )
if event.phase == "began" then
-- Insert touched star into array
table.insert(touchedStarArray, self)
-- Check if array holds 2 stars yet
if table.getn(touchedStarArray) >= 2 then
-- if it does then draw a line between the 2 stars
line = display.newLine( touchedStarArray[1].x, touchedStarArray[1].y, touchedStarArray[2].x, touchedStarArray[1].y)
-- and empty array
touchedStarArray = {}
end
end
end
提前感谢,非常感谢您的帮助 我会输入一些print语句,并确保touchedStarArray[1]确实具有.x属性
\根据您粘贴的代码,我假设您有一个表侦听器,并且它与创建的每个星形对象不同 问题就在这方面
table.insert(touchedStarArray, self)
有很多方法可以解决这个问题
一个非常简单的方法是放入touchedStarArray event.target not self(self是表星,而不是您在createstar函数中创建的星的对象)
table.insert(touchedStarArray, event.target)
另一个解决方案是将监听器放入starcreation函数中
function createstar()
ie = ie - 300
astar = display.newImage('ls.png', math.random( 1, 10) * 33, ie)
astar:addEventListener( "touch", astar)
function astar:touch( event )
if event.phase == "began" then
-- Insert touched star into array
table.insert(touchedStarArray, self)
-- Check if array holds 2 stars yet
if table.getn(touchedStarArray) >= 2 then
-- if it does then draw a line between the 2 stars
line = display.newLine( touchedStarArray[1].x, touchedStarArray[1].y, touchedStarArray[2].x, touchedStarArray[1].y)
-- and empty array
touchedStarArray = {}
end
end
end
physics.addBody(astar)
stars:insert(astar)
sceneGroup:insert(stars)
end
非常感谢Piotr和Rob!!