user3305142
user3305142

Reputation: 227

error loading module '(' expected near '='

This code is to create function listeners for the cannon shooting. When i run the code, it gave me an error 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)
...

I added the listeners to enterScene

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

Upvotes: 1

Views: 1690

Answers (2)

Chomu
Chomu

Reputation: 224

You can not assign two touch listener on the same object. Because it's create conflict to which function it's call first. instead of that, you need to assign one touch and one tap listener. So there is no conflict. background:addEventListener('tap', cannonCharge) background:addEventListener('touch', shot)

Upvotes: 0

Tom Blodget
Tom Blodget

Reputation: 20812

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

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

Try

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

If you don't put local, the variable will be global. Use of globals should be minimized.

If you prefer a more structured syntax, you can use:

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

That's equivalent to:

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

Upvotes: 2

Related Questions