【问题标题】:adding event.phase onto object that randomly spawn.将 event.phase 添加到随机生成的对象上。
【发布时间】:2013-11-09 15:50:13
【问题描述】:

我有四排随机生成的筏子,移动到屏幕的上方然后消失。然后我在木筏下面有水的图像。我已经做到了,当用户走上木筏时,会否定水域重新生成功能。但是,如果仅适用于每行中生成的第一个筏。

local mRandom = math.random
local raft = {"Raft1" ,"Raft2","Raft3"}
local objectTag = 0
local object = {}

function spawnlogright()
    objectTag = objectTag + 1
    local objIdx = mRandom(#raft)
    local objName = raft[objIdx]
    object[objectTag]  = display.newImage(objName..".png")
    object[objectTag].x = 416
    object[objectTag].y = 72
    object[objectTag].name = objectTag
    transition.to(object[objectTag], {time = 10000, x = -96, onComplete = function(obj) obj:removeSelf(); obj = nil; end})
    physics.addBody( object[objectTag], "static", {isSensor = true})
end
spawnlogright()
timer.performWithDelay(3000,spawnlogright,0)

function spawnlogright()
    objectTag = objectTag + 1
    local objIdx = mRandom(#raft)
    local objName = raft[objIdx]
    object[objectTag]  = display.newImage(objName..".png")
    object[objectTag].x = 416
    object[objectTag].y = 168
    object[objectTag].name = objectTag
    transition.to(object[objectTag], {time = 10000, x = -96, onComplete = function(obj) obj:removeSelf(); obj = nil; end})
    physics.addBody( object[objectTag], "static", {isSensor = true})
end
spawnlogright()
timer.performWithDelay(3000,spawnlogright,0)

function spawnlogleft()
    objectTag = objectTag + 1
    local objIdx = mRandom(#raft)
    local objName = raft[objIdx]
    object[objectTag]  = display.newImage(objName..".png")
    object[objectTag].x = -96
    object[objectTag].y = 120
    object[objectTag].name = objectTag
    transition.to(object[objectTag], {time = 10000, x = 416, onComplete = function(obj) obj:removeSelf(); obj = nil; end})
    physics.addBody( object[objectTag], "static", {isSensor = true})
    end
    spawnlogleft()
    timer.performWithDelay(3000,spawnlogleft,0)

function spawnlogleft()
    objectTag = objectTag + 1
    local objIdx = mRandom(#raft)
    local objName = raft[objIdx]
    object[objectTag]  = display.newImage(objName..".png")
    object[objectTag].x = -96
    object[objectTag].y = 216
    object[objectTag].name = objectTag
    transition.to(object[objectTag], {time = 10000, x = 416, onComplete = function(obj) obj:removeSelf(); obj = nil; end})
    physics.addBody( object[objectTag], "static", {isSensor = true})    
end
spawnlogleft()
timer.performWithDelay(3000,spawnlogleft,0)


--while the frog is on the log...
function raftCollide(event)
    if ( event.phase == "began" ) then
        isOnRaft = isOnRaft + 1
    elseif ( event.phase == "ended" )then
        isOnRaft = isOnRaft - 1
end
end

--add event for 'walking on the log'
object[objectTag]:addEventListener("collision",raftCollide)

我如何得到它以便最后一个事件监听器将检测到所有筏子的碰撞

【问题讨论】:

    标签: events lua collision-detection coronasdk spawning


    【解决方案1】:

    如果您已经这样编写代码,那么 objectTag 将指向最后一个 raft,因此只有最后一个对象会添加 eventListener

    你可以这样重新排序

    local mRandom = math.random
    local raft = {"Raft1" ,"Raft2","Raft3"}
    local objectTag = 0
    local object = {}
    
    --MOVE THIS FUNCTION SO WE CAN CALL ON IT LATER
    --while the frog is on the log...
    function raftCollide(event)
        if ( event.phase == "began" ) then
            isOnRaft = isOnRaft + 1
        elseif ( event.phase == "ended" )then
            isOnRaft = isOnRaft - 1
        end
    end
    
    function spawnlogright()
        objectTag = objectTag + 1
        local objIdx = mRandom(#raft)
        local objName = raft[objIdx]
        object[objectTag]  = display.newImage(objName..".png")
        object[objectTag].x = 416
        object[objectTag].y = 72
        object[objectTag].name = objectTag
        transition.to(object[objectTag], {time = 10000, x = -96, onComplete = function(obj) obj:removeSelf(); obj = nil; end})
        physics.addBody( object[objectTag], "static", {isSensor = true})
        --ADD EVENT LISTENER TO EACH LOG
        --add event for 'walking on the log'
        object[objectTag]:addEventListener("collision",raftCollide)
    end
    spawnlogright()
    timer.performWithDelay(3000,spawnlogright,0)
    --AND SO ON WITH MORE LOGS....
    

    计时器功能是否生成日志?您可能需要向 spawn 函数添加一个事件参数。此外,尝试将所有生成物放在一个函数中,这样您就可以更好地控制将筏子插入的索引

    【讨论】:

    • 非常感谢。工作。花了几个小时试图找到解决方案。我对 lua 很陌生
    猜你喜欢
    • 2019-08-06
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2019-08-02
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多