【问题标题】:error loading module '(' expected near '='错误加载模块“(”预计在“=”附近
【发布时间】:2014-03-20 12:26:20
【问题描述】:

这段代码是为炮击创建函数监听器。当我运行代码时,它给了我一个错误 Question1.lua:43 '('expected near '='

function cannonCharge = function(event)
  if(event.phase == 'began') then
        impulse = 0
        cannon.isVisible = true
        Runtime:addEventListener('enterFrame', charge)
        print ('cannonCharge')
    end
end

function shot = function(event)
    if(event.phase == 'ended') then

        Runtime:removeEventListener('enterFrame', charge)
        cannon.isVisible = true
        cannon.rotation = 0

        cannonBall = display.newImage('cannon ball.png', 84, 220)
        physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})
        cannonBalls:insert(cannonBall)
        print ('shot')

-- Shoot cannon ball
cannonBall:applyLinearImpulse(3, impulse, cannonBall.x, cannonBall.y )

--Collision listener
cannonBall:addEventListener ('collision', ballCollision)

    end
end

function scene:createScene(event)
...

我添加了监听器进入场景

function scene:enterScene( event )
local group = self.view
background:addEventListener('touch', cannonCharge)
background:addEventListener('touch', shot) 
  end

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    变量没有类型;只有价值观可以。所以,而不是

    function shot = function(event)
    -- ...
    end
    

    试试

    local shot = function(event)
    -- ...
    end
    

    如果你不输入local,变量将是全局的。应尽量减少使用全局变量。

    如果你喜欢更结构化的语法,你可以使用:

    local function shot(event)
    -- ...
    end
    

    相当于:

    local shot
    shot = function(event)
    -- ...
    end
    

    【讨论】:

    • 我将 cannonCharge 和 shot 都更改为本地,但它给了我一个错误,“断言失败”。
    • 那很好。您已经克服了语法错误。现在你有一个新问题。当前问题没有足够的细节来说明新问题。
    【解决方案2】:

    您不能在同一个对象上分配两个触摸侦听器。因为它会与首先调用哪个函数产生冲突。 取而代之的是,您需要分配一键式和一键式侦听器。所以没有冲突。 背景:addEventListener('点击',cannonCharge) 背景:addEventListener('touch', shot)

    【讨论】:

    • 如果我不使用触摸来进行cannonCharge,大炮将不会旋转而只会向前射击。切换它们会给我错误断言失败。
    • 你可以在 enterFrame 或 Runtime 监听器中旋转你的大炮。
    • 你能告诉我你在什么时候得到 '('expected near '=' 这个错误。我找不到第 43 行。
    猜你喜欢
    • 2020-09-15
    • 2019-01-11
    • 2022-11-30
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2020-12-12
    相关资源
    最近更新 更多