2014-07-04 62 views
0

我生成了一些矩形,每個矩形都轉換爲向下移動屏幕。我試圖從交易完成的內存中刪除這些矩形(意味着向下移動屏幕並在屏幕上不可見的矩形)。但相反,可見的(新產生的)矩形被刪除。在轉換完成時刪除對象

這裏是我的代碼

--table to hold dynamically created rectangles 
local rects={} 
--used as index for table 
local numRect=0 

local function removeRect(obj) 
    local rectid = obj.id 

    obj:removeSelf() 
    rects[rectid] = nil 
end 

--function for spawning rectangles 
local function spawnRect() 
    numRect = numRect + 1 
    rects[numRect] = display.newRect(display.contentWidth/2, 100, 100, 100) 
    rects[numRect]:setFillColor(1,1,1) 
    rects[numRect].id = numRect 

    transition.to(rects[numRect], {time = 9000, y = display.contentHeight + 100, 
      onComplete = function() 
       removeRect(rects[numRect]) 
      end 

    }) 
end 

timer.performWithDelay(1000, spawnRect, -1) 

回答

2

的聽衆的onComplete已經收到,目前正在轉移的對象。所以,你不需要通過它。

只要改變你的transition.to擁有的onComplete = removeRect,如下圖所示:

transition.to(rects[numRect], {time = 9000, y = display.contentHeight + 100, 
     onComplete = removeRect })