0

我想让一个精灵图像与硬币发生碰撞。硬币是一个图像,它总是因为这段代码而移动:

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

有谁知道我如何与硬币图像发生碰撞?

提前致谢。

4

1 回答 1

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)

如果你想要与硬币和精灵对象的特定碰撞,你应该使用碰撞过滤器。

于 2012-11-09T05:52:27.513 回答