lua事件监听器调用方法四次而不是一次

在我的游戏中(使用Corona SDK),我想要每三秒钟召唤一个敌人。我的spawnBlob一次只创建一个blob,但是每三秒钟就会出现四个在屏幕上。我对lua和corona还是新手,并且在追踪这段代码并找出为什么会调用四次而不应该这样做时遇到了麻烦。我在碰撞检测中也遇到了这个问题,我打印了两个物体碰撞的位置。但是当两个物体碰撞时,打印语句的4行都会被打印出来,而我不知道发生了什么。

我是否应该像触摸事件一样使用一个计时器的事件阶段?

local allBlobs = {} --全局变量

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 ) 包含mainGroup,spriteSheets和buttons

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

--scene:hide (event )为空
--scene:destroy ( event )为空

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

return scene
点赞
用户10378041
用户10378041

scene:show 函数中,您可以使用以下代码:

local phase = event.phase

if (phase == "will") then
   --调用您的监听器
elseif (phase == "did") then
end
2019-03-18 04:02:41