【问题标题】:How can I fix this error? (Corona SDK)我该如何解决这个错误? (电晕SDK)
【发布时间】: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 函数?
  • 如果你想对同一个对象进行操作,没有。您想执行一次并在两个函数中使用相同的对象。但我不确定你在做什么,所以我不能具体说明你需要做什么。

标签: lua coronasdk


【解决方案1】:

我认为这是您声明函数的方式有点错误。您在rotateBasket 中声明了显示对象basket,这将使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

【讨论】:

    【解决方案2】:

    这里你在函数内部声明本地篮子并尝试插入到函数外部的场景组中,因为它是本地的,无法在函数外部访问。

    --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 )
    

    【讨论】:

      猜你喜欢
      • 2016-07-21
      • 2016-09-09
      • 2015-02-28
      • 2013-08-16
      • 2020-04-17
      • 1970-01-01
      相关资源
      最近更新 更多