object pooling

hii…to everyone who is reading this post.

i have a simple question but yet i dont have any answer .i am reading  about spawning objects by creating pool  and how to reuse our objects  by this article

http://gamesbycandlelight.com/object-pooling-in-coronasdk/

i found  out that how to create many obstacle from a single image and than insert it in a table to reuse but my confusion is ,what would happen if some of our obstacles created by using two or three different object by using  PHYSICS JOINTS. is that particular obstacle considered as one physical object or we have suppose every object of that obstacle distinct. and second problem is how to spawn that obstacle and use property like 

isbodyactive  and isVisible.

hope someone understand what i want to say

Thanks for checking out my article!

I’m writing this without really testing the code, but it should give you an idea what to do.  I am going to find some time to look into this in more detail and will write an updated article with a working example.  This is a great intermediate level application of pooling.  

Basically, in the creation loop, create your objects and joints, and store everything into the master pooling table.  So, using my example, I would do something like this:

local maxEnemies = 50 local Enemies = {} for i = 1, maxEnemies do local obj1 = display.newImage("object1.png", 0, 0) physics.addBody(obj1, "dynamic", params) obj1.isVisible = false local obj2 = display.newImage("object2.png", 10, 10) physics.addBody(obj2, "dynamic", params) obj2.isVisible = false local pivotJoint = physics.newJoint( "pivot", obj1, obj2, anchor\_x, anchor\_y ) Enemies[i].obj1 = obj1 Enemies[i].obj2 = obj2 Enemies[i].pivotJoint = pivotJoint Enemies[i].isAlive = false end 

You can then spawn a jointed object via pooling, but you will still have to handle the objects and the joint, just like you would if you were not pooling. 

-- return an available enemy, or nil if there is no enemies left local function spawnEnemy() for i = 1, #Enemies do if not Enemies[i].isAlive then return Enemies[i] end end -- if we get here, there are no more available enemies in the pool return nil end local enemy = spawnEnemy() if enemy ~= nil then -- enemy would be a table consisting of: -- enemy.obj1 -- enemy.obj2 -- enemy.pivotJoint -- enemy.isAlive end

Hope this helps… and I hope the code is close to functional.  Like I stated, the code above is untested, but I think it will keep you going.  I will spend some time in the near future to expand my article.

–John

@schizoid2k

thanks a lot…this will prove very supportive.waiting for your article.

Thanks for checking out my article!

I’m writing this without really testing the code, but it should give you an idea what to do.  I am going to find some time to look into this in more detail and will write an updated article with a working example.  This is a great intermediate level application of pooling.  

Basically, in the creation loop, create your objects and joints, and store everything into the master pooling table.  So, using my example, I would do something like this:

local maxEnemies = 50 local Enemies = {} for i = 1, maxEnemies do local obj1 = display.newImage("object1.png", 0, 0) physics.addBody(obj1, "dynamic", params) obj1.isVisible = false local obj2 = display.newImage("object2.png", 10, 10) physics.addBody(obj2, "dynamic", params) obj2.isVisible = false local pivotJoint = physics.newJoint( "pivot", obj1, obj2, anchor\_x, anchor\_y ) Enemies[i].obj1 = obj1 Enemies[i].obj2 = obj2 Enemies[i].pivotJoint = pivotJoint Enemies[i].isAlive = false end 

You can then spawn a jointed object via pooling, but you will still have to handle the objects and the joint, just like you would if you were not pooling. 

-- return an available enemy, or nil if there is no enemies left local function spawnEnemy() for i = 1, #Enemies do if not Enemies[i].isAlive then return Enemies[i] end end -- if we get here, there are no more available enemies in the pool return nil end local enemy = spawnEnemy() if enemy ~= nil then -- enemy would be a table consisting of: -- enemy.obj1 -- enemy.obj2 -- enemy.pivotJoint -- enemy.isAlive end

Hope this helps… and I hope the code is close to functional.  Like I stated, the code above is untested, but I think it will keep you going.  I will spend some time in the near future to expand my article.

–John

@schizoid2k

thanks a lot…this will prove very supportive.waiting for your article.