Nick97832954
Nick97832954

Reputation: 149

lua eventListener is calling method four times instead of just once

In my game (using Corona SDK) I want to spawn an enemy every 3 seconds. My spawnBlob creates only 1 blob at a time but four are appearing on screen at once every 3 seconds. I'm new at lua and corona and am having trouble tracing this code and figuring how things are being called four times when they shouldn't be. I am also having this problem with collision detection where i print the position where two objects collide. When two objects collide however, 4 lines of the print statement get printed and I don't know what is going on.

Is there an event.phase for this timer I should be utilizing similar to the began for touch events?

local allBlobs = {} -- global variable

function spawnBlob( event )
    allBlobs[#allBlobs + 1] = display.newSprite ( mainGroup, mySheet3, 
    sequenceDataBlob)
    local blob = allBlobs[#allBlobs]
    physics.addBody( blob, { density=0.3, friction=0.6 })
    blob.x = math.random(0, display.contentWidth)
    blob.y = -80
    blob.myName = "blob" .. #allBlobs
    physics.addBody(blob, "dynamic", {density=0.1, bounce=0, friction=.2, 
    radius=128})
    blob:play()

end

--scene:create( event ) contains mainGroup, spriteSheets and buttons

timer.performWithDelay( 3000, spawnBlob, 0) --in scene:show(event) 

--scene:hide (event ) is empty
--scene:destroy ( event ) is empty

scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("hide", scene)
scene:addEventListener("destroy", scene)

return scene

Upvotes: 1

Views: 71

Answers (1)

Nick97832954
Nick97832954

Reputation: 149

In the scene:show function you can use

local phase = event.phase

if (phase == "will") then
   --call your listeners
elseif (phase == "did") then
end

Upvotes: 1

Related Questions