【问题标题】:set a event listener collision for all image in a list.为列表中的所有图像设置事件侦听器冲突。
【发布时间】:2013-11-26 02:46:35
【问题描述】:

我有一个部分可以从一个列表中随机生成图像,然后在屏幕上生成它们并连续消失。生成的图像被放入一个名为 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。这将否定水死亡功能。有没有办法将碰撞检测添加到对象列表中的所有筏或所有实体。

任何帮助将不胜感激。

【问题讨论】:

    标签: list lua collision-detection coronasdk addeventlistener


    【解决方案1】:

    只需将最后一行替换为:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 2020-08-27
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      相关资源
      最近更新 更多