0

我有一个部分可以从一个列表中随机生成图像并在屏幕上生成它们并不断地消失它们。生成的图像被放入一个名为 object 的列表中。我试图让屏幕上生成的每个图像都有用户精灵的碰撞检测。然而,目前它只检测到产生的第一个筏。

isOnRaft = 0

--Set log position and movement
local mRandom = math.random
local raft = {"Raft1" ,"Raft2"}
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(3500,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(3500,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(3500,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(3500,spawnlogleft,0)

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

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

所以我需要用户精灵来检测所有产生的筏子,并将 1 添加到 isOnRaft。这将否定水死亡功能。有没有办法将碰撞检测添加到对象列表中的所有筏或所有实体。

任何帮助将不胜感激。

4

1 回答 1

1

只需将最后一行替换为:

for logTag, logObject in pairs(object) do
    logObject:addEventListener("collision", raftCollide)
end

也只是一个建议,但做你想做的事:尽量不要在同一范围内声明 2 个具有相同名称的函数......你应该将第二个 spawnRight / spawnLeft 函数的名称更改为具有不同的名称:)

于 2013-11-13T14:25:38.943 回答