0

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

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)

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

4

1 回答 1

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 函数添加一个事件参数。此外,尝试将所有生成物放在一个函数中,这样您就可以更好地控制要插入筏的索引

于 2013-11-13T06:55:20.343 回答