加载模块时出错('='附近应该有'(')

这段代码用于创建大炮开火的函数侦听器。当我运行代码时,它给了我一个错误 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')

-- 发射大炮球
cannonBall:applyLinearImpulse(3, impulse, cannonBall.x, cannonBall.y )

-- 碰撞侦听器
cannonBall:addEventListener ('collision', ballCollision)

    end
end

function scene:createScene(event)
...

我在enterScene中添加了侦听器

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

Variables don't have types; Only values do. So, instead of

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

Try

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

如果您不加 local,那么这个变量就会是全局变量。应该尽量减少使用全局变量。

如果您喜欢更有结构的语法,可以尝试:

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

这相当于:

local shot
shot = function(event)
-- ...
end
2014-03-20 00:23:06
用户2409015
用户2409015

你不能在同一个对象上分配两个 触摸监听器。因为这会导致冲突,无法确定首先调用哪个函数。 相反,你需要分配一个 触摸监听器 和一个 点击监听器。这样就不会发生冲突了。

 background:addEventListener('tap', cannonCharge)
 background:addEventListener('touch', shot)
2014-03-20 04:26:20