【发布时间】:2015-03-17 23:02:56
【问题描述】:
所以我从这组代码中得到了这个错误,我想知道我可以做些什么来完成这个工作。错误显示“错误:预期的表。如果这是一个函数调用,您可能使用了'。'而不是 ':'" 我还在学习 lua,所以这让我很困惑,如果你能提供答案,我将不胜感激。
function scene:create( event )
--Declared variables
local sceneGroup = self.view
local background = display.newImage( "game_background.jpg", display.contentWidth, display.contentHeight )
background.anchorX = 0
background.anchorY = 0
background.x, background.y = 0, 0
--Rotation Function for Object "Basket"
local function rotateBasket(event)
--Declared Variables inside rotation function
local t = event.target
local phase = event.phase
local basket = display.newImageRect("basket.jpg" , 90, 60)
basket.x = display.contentCenterX
basket.y = display.contentCenterY
--Rotation
if (phase == "began") then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x1 = event.x
t.y1 = event.y
elseif t.isFocus then
if (phase == "moved") then
t.x2 = event.x
t.y2 = event.y
angle1 = 180/math.pi * math.atan2(t.y1 - t.y , t.x1 - t.x)
angle2 = 180/math.pi * math.atan2(t.y2 - t.y , t.x2 - t.x)
rotationAmt = angle1 - angle2
t.rotation = t.rotation - rotationAmt
t.x1 = t.x2
t.y1 = t.y2
elseif (phase == "ended") then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
--Event Listener
basket:addEventListener("touch", rotateBasket)
return true
end
--sceneGroup insertions
sceneGroup:insert( background )
sceneGroup:insert( basket )
end
【问题讨论】:
-
您是否有任何迹象表明该代码中的 where 错误来自?一条线?一个阶段?有什么事吗?
-
是的,它说错误来自靠近场景组插入的地方。
-
您的
basket变量是rotateBasket函数的本地变量。它在您尝试使用它的scene:create函数中不存在。 -
所以我必须申报两次吗?在场景中:创建函数和 rotateBasket 函数?
-
如果你想对同一个对象进行操作,没有。您想执行一次并在两个函数中使用相同的对象。但我不确定你在做什么,所以我不能具体说明你需要做什么。