kevin ver
kevin ver

Reputation: 851

Removing specific objects on Collision

removing the coin im colliding whit seems a bit of a problem im got this :

local screenGroup = self.view
local options2 =
{
    --required parameters
    width = 16,
    height = 16,
    numFrames = 8,

    --optional parameters; used for dynamic resolution support
    sheetContentWidth = 128,  -- width of original 1x size of entire sheet
    sheetContentHeight = 16,  -- height of original 1x size of entire sheet
}

local imageSheet1 = graphics.newImageSheet( "items/coin.png", options2 )
-- Example assumes 'imageSheet' is already created using graphics.newImageSheet()

-- non-consecutive frames
local sequenceData1 =
{
    name="normal",
    frames= {1,2,3,4,5,6,7,8}, -- frame indexes of animation, in image sheet
    time = 600, --700           -- Optional, in milliseconds ; if not supplied, the animation is frame-based
    loopCount = 0        -- Optional ; default is 0
}

local coin = {}

local coinspawn = function()
    local i = display.newSprite( imageSheet1, sequenceData1 )

    i.x = display.contentWidth/2
    i.y = display.contentHeight/2
    i:play()
    i.collided = true
    i.name = "coin"
    physics.addBody(i, "dynamic", 
        {density=.1, bounce=0.1, friction=.2, shape= shape2 ,filter=playerCollisionFilter } 
    )   
    --player.gravityScale = 0.5
    coinIntro = transition.to(i,{time=2000, x=display.contentWidth/2-50 ,onComplete=jetReady , transition=easing.OutExpo } ) --
    coin[#coin+1] = i

end 
timer.performWithDelay( 1000, coinspawn, 0 )

function coinPlus()
    for i = #coin, 1, -1 do
        if coin[i] ~= nil then
            local function dellcoin()
                if coin[i] ~= nil then
                    coin[i]:removeSelf()
                    coin[i] = nil
                end
            end
            transition.to( coin[i], {  time=100, alpha=0, onComplete = dellcoin} )
            break
        end
    end
end

local function onCollision(event)
    if event.phase == "began" and gameIsActive == true then
        local obj1 = event.object1; 
        local obj2 = event.object2; 

        if obj1.name == "playerpop" then
            if     obj2.name == "BGfrontFL1" then --helper()
            elseif obj2.name == "BGfrontFL2" then --helper()
            elseif obj2.name == "coin"   then coinPlus()
            end
        end
    end
end

Runtime:addEventListener( "collision", onCollision )

withs kinda works but it removes the last spawned coin and not the one in collision, how can i fix this ?

Upvotes: 0

Views: 506

Answers (1)

Oliver
Oliver

Reputation: 29493

In coinspawn you create the coin and add it to coin table. It appears that your coin table will contain all spawned coins that have not been collided with (that seems to be your intention anyways). Then when a coin collides the onCollision() will get called, which will call coinPlus(). The latter then loops over all coins in coin table, starting with the latest spawned one (at end of table), and if it is not nil it starts a fade out with removal on completion of fade-out. This is surely not what you intend: you want to delete only the coin collided.

So the biggest problem is the way the coin that was involved in collision gets removed: looping over all coins, don't think it is necessary. You should try passing coin as arg to coinPlus:

if obj1.name == "playerpop" then
    if     obj2.name == "BGfrontFL1" then --helper()
    elseif obj2.name == "BGfrontFL2" then --helper()
    elseif obj2.name == "coin"   then coinPlus(obj2)
    end
end

function coinPlus(coinToRemove)
    local function dellcoin()
        coinToRemove:removeSelf()
        for i, coin in ipairs(coin) do
            if coin == coinToRemove then 
                coin[i] = nil
                break
            end
        end
    end
    transition.to( coinToRemove, {  time=100, alpha=0, onComplete = dellcoin} )
end

Another problem is your use of '#' operator: it is only to be used with tables that don't have "holes", so if you really do remove individual entries from your table (thus creating holes), then #coin will no longer work (that's why I used ipairs).

Upvotes: 1

Related Questions