0
votes

I want to make a sprite image have a collision with the coin. The coin is a image and it is always moving because of this code:

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

Does anyone know how I can have a collision with the coin image?

Thanks in advance.

1
Not sure I understand. Do you want to detect collisions? - Chris Gerken
yes i do want to detect collisions - Cyrus Vachha

1 Answers

0
votes

Add collision event listener to the coin like this,

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)

if you wants the specific collision with the coin and sprite object,you should use collision filter.