【问题标题】:lua eventListener is calling method four times instead of just oncelua eventListener 调用方法四次而不是一次
【发布时间】:2019-08-06 21:59:27
【问题描述】:

在我的游戏中(使用 Corona SDK),我想每 3 秒生成一个敌人。我的 spawnBlob 一次只创建 1 个 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 coronasdk


    【解决方案1】:

    在场景中:你可以使用的显示功能

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 2022-08-18
      相关资源
      最近更新 更多