2012-11-05 91 views
0

我想讓一個精靈圖像與硬幣碰撞。 硬幣是一個形象,因爲這個代碼是永遠向前:Corona SDK Lua-與硬幣碰撞

local tPrevious = system.getTimer(); 
local function move(event) 

    local tDelta = event.time - tPrevious; 
    tPrevious = event.time; 

    local xOffset = (0.3 * tDelta); 

    grass.x = grass.x - xOffset; 
    grass2.x = grass2.x - xOffset; 
    coin.x = coin.x - xOffset; 
    if (grass.x + grass.contentWidth) < 0 then 
     grass:translate(480 * 2, 0); 
    end 
    if (grass2.x + grass2.contentWidth) < 0 then 
     grass2:translate(480 * 2, 0); 
    end 

    if (coin.x + coin.contentWidth) < 0 then 
     coin:translate(480 * 2, 0); 
     coinRect.x = coin.x 
    end 

    local i; 

end 

有誰知道我怎麼能有硬幣圖像的碰撞?

在此先感謝。

+1

不知道我明白。你想檢測碰撞? –

+0

是的,我想檢測碰撞 –

回答

0

添加碰撞事件偵聽器,硬幣這樣,

local function onLocalCollision(self, event) 
if (event.phase == "began") then 

     print("on collision began") 

elseif (event.phase == "ended") then 

    print("on collision ended") 

end 
end 
coin:addEventListener("collision", onLocalCollision) 
,如果你想用硬幣和精靈對象的特定碰撞

,你應該使用碰撞過濾器。

+0

什麼是碰撞過濾器 –

+0

碰撞過濾器是決定哪些物體必須相互碰撞的碰撞過濾器。 – subaja