2013-11-09 32 views
0

我有四排隨機產生的木筏,移動到屏幕的上方,然後消失。然後在木筏下面有水圖像。我做到了這一點,當用戶走上筏子否定水域重新生成功能。但是,如果只適用於每排產卵的第一排筏。將event.phase添加到隨機生成的對象上。

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

的碰撞,如果你寫的代碼這樣,那麼objectTag將指向最後一個木筏,所以只最後目標是要具有事件監聽添加到它

,你可以重新排序像這樣

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函數添加一個事件參數。另外,試着將所有的產物放在一個函數中,這會讓你更好地控制你插入木筏的索引。

+0

非常感謝。工作。花了數小時試圖找到解決辦法。我對盧亞頗爲陌生 – Ross

相關問題