2014-02-07 133 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)。