将event.phase添加到随机生成的对象上。

时间:2013-11-09 15:50:13

标签: events lua collision-detection corona spawning

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

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)

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

1 个答案:

答案 0 :(得分:1)

如果您编写了类似的代码,那么objectTag将指向最后一个木筏,所以只有最后一个对象才会添加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函数添加一个事件参数。另外,尝试将所有产生的东西放在一个函数中,这样可以让你更好地控制你将木筏插入的索引