lua eventListener调用方法四次而不是一次

问题描述 投票:1回答:1

在我的游戏中(使用Corona SDK)我想每3秒产生一个敌人。我的spawnBlob一次只创建一个blob,但是每隔3秒就会有四个出现在屏幕上。我是lua和corona的新手,我在查找这些代码时遇到了麻烦,并且在他们不应该这样做的时候想出了四次被调用的东西。我也遇到了碰撞检测的问题,我打印了两个物体碰撞的位置。但是当两个对象发生碰撞时,打印出4行print语句,我不知道发生了什么。

这个计时器是否有一个event.phase我应该使用类似于触摸事件的开始?

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

lua corona
1个回答
1
投票

在场景中:你可以使用show功能

local phase = event.phase

if (phase == "will") then
   --call your listeners
elseif (phase == "did") then
end
© www.soinside.com 2019 - 2024. All rights reserved.