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