First, please run below code:
sceneGame = cocos2d.CCScene:node()
icons = {}
function validateIcon(icon)
if icon._isUsed then
print("USED ICON:", icon)
else
print(" NEW:", icon)
icon._isUsed = true
end
end
-- create icons, append to scene
function createIcons(num)
for i = 1, num or 30 do
local icon = cocos2d.CCSprite:spriteWithFile("CloseNormal.png")
icon:setPosition(cocos2d.CCPoint(math.random(20, 460), math.random(-350, -30)))
validateIcon(icon)
icons[#icons + 1] = icon
sceneGame:addChild(icon)
end
end
function tick()
for i = #icons, 1, -1 do
local icon = icons[i]
local pos = icon:getPosition()
pos.y = pos.y + 3
if pos.y > 340 then
sceneGame:removeChild(icon, false)
table.remove(icons, i)
createIcons(1)
else
icon:setPosition(pos)
end
end
end
createIcons()
cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 1.0 / 60, false)
cocos2d.CCDirector:sharedDirector():runWithScene(sceneGame)
Check debug output:
NEW: userdata: 0x5e48ba4
NEW: userdata: 0x5e4b034
NEW: userdata: 0x5e4abc4
NEW: userdata: 0x5e4b354
NEW: userdata: 0x5e4b014 <---
NEW: userdata: 0x5e4adc4
NEW: userdata: 0x5e4c394
NEW: userdata: 0x5e4c824
NEW: userdata: 0x5e4cbe4
NEW: userdata: 0x5e4bdf4
NEW: userdata: 0x5e4d8e4
NEW: userdata: 0x5e4dd64 <---
NEW: userdata: 0x5e4e1f4
NEW: userdata: 0x5e4e494
NEW: userdata: 0x5e47e74
NEW: userdata: 0x5e4ef64
NEW: userdata: 0x5e4f3f4
NEW: userdata: 0x5e4f774
NEW: userdata: 0x5e4d224
NEW: userdata: 0x5e4fe94
NEW: userdata: 0x5e50324
NEW: userdata: 0x5e507b4
NEW: userdata: 0x5e4d314
NEW: userdata: 0x5e4d484 <---
NEW: userdata: 0x5e50924
NEW: userdata: 0x5e519b4
NEW: userdata: 0x5e51e44
NEW: userdata: 0x5e522b4
NEW: userdata: 0x5e52744 <---
NEW: userdata: 0x5e529f4
NEW: userdata: 0x5855574
USED ICON: userdata: 0x5e4dd64
USED ICON: userdata: 0x5e4b014
USED ICON: userdata: 0x5e52744
USED ICON: userdata: 0x5e4d484
OK, problems found. “icon” objects reuse same memory address.