删除碰撞上的特定对象

时间:2014-02-07 19:08:05

标签: lua corona

移除硬币即时碰撞白衣似乎有点问题我得到了这个:

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 )

有点工作,但它删除了最后生成的硬币,而不是碰撞中的硬币,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

coinspawn中,您可以创建硬币并将其添加到coin表格中。您的coin表格似乎包含所有未与之相撞的衍生硬币(无论如何这似乎都是您的意图)。然后,当硬币碰撞时,onCollision()将被调用,这将调用coinPlus()。然后后者循环遍历coin表中的所有硬币,从最新产生的硬币(在表的末尾)开始,如果它不是零,则在淡出完成时开始淡出淡出。这肯定不是你想要的:你只想删除碰撞的硬币。

所以最大的问题是碰撞中涉及的硬币被移除的方式:循环所有硬币,不认为有必要。你应该尝试将硬币作为arg传递给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

另一个问题是你使用'#'运算符:它只用于没有“hole”的表,所以如果你确实从表中删除了单个条目(从而创建了空洞),那么#硬币将不再有效(这就是我使用ipairs)的原因。