2014-02-07 62 views
0

删除特定对象取出币IM碰撞白衣似乎有点问题即时的得到这个:的碰撞

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有点儿工作,但它消除了最后催生了硬币,而不是一个碰撞,如何能我解决这个问题?

回答

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 

的另一个问题是你的“#”运营商的应用:它是只与没有“孔”表中使用,所以如果你真的这样做从桌面上删除单个条目(从而创建漏洞),那么#币将不再起作用(这就是为什么我使用了ipairs)。