我该如何解决这个错误? (Corona SDK)

时间:2015-01-19 20:41:12

标签: lua corona

所以我从这个代码中得到了这个错误,我想知道我能做些什么来使这个工作。错误表示" ERROR:表格预期。如果这是一个函数调用,您可能已经使用了'。'而不是'''"我还在学习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

2 个答案:

答案 0 :(得分:0)

我认为这是你宣布这个函数有点错误的方式。您在basket内声明显示对象rotateBasket,这会使basket在该功能之外无法访问。我假设您要声明要旋转的背景和篮子图像,因此,假设您的rotateBasket函数是正确的,您可以尝试这样的事情:

--Rotation Function for Object "Basket"
local function rotateBasket(event)
    --Declared Variables inside rotation function
    local t = event.target
    local phase = event.phase

    --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
    return true
end

function scene:create( event )
    --Declared variables
    local sceneGroup = self.view

    --Create background
    local background = display.newImage( "game_background.jpg", display.contentWidth, display.contentHeight )
    background.anchorX = 0
    background.anchorY = 0
    background.x, background.y = 0, 0

    --Create basket 
    local basket = display.newImageRect("basket.jpg" , 90, 60)
    basket.x = display.contentCenterX
    basket.y = display.contentCenterY

    --Add event listener to basket (may also be done in scene:show function)
    basket:addEventListener("touch", rotateBasket)  

    --sceneGroup insertions
    sceneGroup:insert( background )
    sceneGroup:insert( basket )
end

答案 1 :(得分: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
 sceneGroup:insert( basket )
 --Event Listener
 basket:addEventListener("touch", rotateBasket)
 return true
end
 --sceneGroup insertions
 sceneGroup:insert( background )