Great, hmm, how about instead of simply removing items, you cache them and reuse them? That way it will be more efficient because there won't be clogging a memory with removed items and no overhead of allocating memory to new items
Great, hmm, how about instead of simply removing items, you cache them and reuse them? That way it will be more efficient because there won't be clogging a memory with removed items and no overhead of allocating memory to new items
@ar2rsawseen, would be something like the example that comes with Gideros. (Bird Animation) only there are four birds!
Yes, well the pool with unlimited amount of objects could be like this:
Pool = Core.class(Sprite)function Pool:init()
self.pool ={}endfunction Pool:createObject()local b
--if there is anything in pool take itif #self.pool >0then
b =table.remove(self.pool)else
b = Bitmap.new(Texture.new("image.png", true))end
stage:addChild(b)return b
endfunction Pool:destroyObject(b)
b:removeFromParent()table.insert(#self.pool, b)end
Here is the implementation I wrote for a game I'm working on. Hopefully someone will find it useful.
--[[
Storage to recycle bitmaps for use later.
Originally written by Brant D. Thomsen
Contributed to the public domain
]]local bitmapList ={}-- Returns the bitmapList index for the texture, or 0 if not found.-- Does a binary search for efficiency.localfunction findTextureIndex(textureString)local low =1local high = #bitmapList
local mid
while low < high do
mid =math.floor((low + high)/2)if textureString < bitmapList[mid][1]then
high = mid - 1elseif textureString > bitmapList[mid][1]then
low = mid + 1elsereturn mid
endendif low == high and textureString == bitmapList[low][1]thenreturn low
endreturn0end-- Get a bitmap from the pool, if there is one there.-- Otherwise, a new bitmap is generated from the supplied texture.function getBitmapFromPool(texture)-- Return a bitmap if we already have one.local index = findTextureIndex(tostring(texture))if index >0and #bitmapList[index][2]>0thenreturntable.remove(bitmapList[index][2], #bitmapList[index][2])end-- Create a new bitmap and return it.return Bitmap.new(texture)end-- Add a bitmap with the supplied texture back to the pool.-- Removes the bitmap from the object holding it.function putBitmapBackIntoPool(texture, bitmap)assert(texture)assert(bitmap)
bitmap:removeFromParent()
bitmap:setAlpha(1.0)-- Add to the pool, if there is already a place for it.local textureString =tostring(texture)local index = findTextureIndex(textureString)if index >0thentable.insert(bitmapList[index][2], bitmap)returnend-- Create a new texture list and add it to the pool.-- Insert it in the sorted position to allow for binary searches later.
index =1while index <= #bitmapList and textureString > bitmapList[index][1]do
index = index + 1endtable.insert(bitmapList, index, {textureString, {bitmap}})assert(index ==1or bitmapList[index - 1][1]< textureString)assert(index == #bitmapList or bitmapList[index + 1][1]> textureString)end
Comments
hmm, how about instead of simply removing items, you cache them and reuse them? That way it will be more efficient because there won't be clogging a memory with removed items and no overhead of allocating memory to new items
Likes: vitalitymobile, luyimoon
Likes: vitalitymobile
[-] Liasoft
Likes: HubertRonald, vitalitymobile, DungDajHjep, SinisterSoft, pie, antix, MoKaLux