Pregunta

i have four rows of rafts that randomly spawn, move to the over side of the screen and then disappear. im then have the water image underneath the rafts. i have made it so that when the user walks onto the raft in negates the waters re-spawn function. however if only works for the first raft that is spawned in each row.

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)

how do i get it so that the last Event Listener will detect the collision of all the rafts

¿Fue útil?

Solución

if you have written your code like that, then objectTag will point to the last raft, so only the last object is going to have the eventListener added to it

you could reorder it like this

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....

does the timer function spawn the logs? you may need to add an event argument to the spawn functions. Also, try and put all the spawns in one function, that'll give you more control over the index you're inserting the rafts into

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top