سؤال

I am making a game with a guy that collects things, like coins. I want to detect the collision between these two, so I can remove the coin, but I don't want the coin to interact with the character, because right now it is slowing him down slightly. It should still interact with the ground before the collision though. Thanks for your help!

function createCoin()
    for i = 1, 10 do
        coin = display.newCircle(0, 0, 16)
        coin.x = totallength - 1000 + i * 100
        coin.y = totalheight - 200

        physics.addBody(coin,
            {bounce = 0, friction = 1, density = 0}
        )

        game:insert(coin)
        coin.myName = "coin"
    end
end

createCoin()

local function onCollision(event)
    if event.phase == "began" then
        if (event.object1.myName == "coin" and
                event.object2.myName == "wheel") then

            event.object1:removeSelf();     
        end 
    end
end
هل كانت مفيدة؟

المحلول

You cannot remove objects involved in a collision during the collision handling: see Modifying Objects" at Collision event page. Use timer.performWithDelay() as documented. This should prevent your coin from interacting with player. If that doesn't work, you could create a "ghost" object that follows the coin everywhere (same size placement etc but not visible) and is added to physics as a sensor. A sensor does not cause collision dynamics but the event is fired. You would also need to do a delayed removal of coin if removal is desired.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top