Best practice to repeat code

Ok In my code i have set up a caterpillar which you drag to a leaf… He eats the leaf and then you drag the caterpillar to a black box which then triggers the caterpillar to build a cocoon… You can then touch the cocoon and a butterfly comes out. You can drag the butterfly around and he flys… I want to know how I can repeat this code 5 times so I have 5 caterpillars, 5 leafs, 5 cocoons and 5 butterflies… How can I do this in OOP… Put this whole thing in its own .lua file?

[lua]display.setStatusBar (display.HiddenStatusBar)
–> Hides the status bar

local _W = display.contentWidth
local _H = display.contentHeight

local loqsprite = require(‘loq_sprite’)
local BG = display.newImageRect ( “caterpillerBackground.png”, 1024, 768 )
BG.x = _W/2; BG.y = _H/2

loqsprite.addScale(2)

local lfactory = loqsprite.newFactory(‘leaf’)
local leaf = lfactory:newSpriteGroup()
leaf.x = 250; leaf.y = 300
–leaf:setFillColor(255,0,0)
local box = display.newRect (0, 0, 80, 80)
box.x = 250; box.y = 300
box.alpha = 0

local box2 = display.newRect (0, 0, 80, 80)
box2.x = 300; box2.y = 450
box2:setFillColor (0, 0, 0)
box2.alpha = 0
local cfactory = loqsprite.newFactory(‘caterpillar’)
local caterpillar = cfactory:newSpriteGroup()
caterpillar.x = 500; caterpillar.y = 650
caterpillar:play(“aniCatipilar still”)

function hitTestObjects(obj1, obj2)
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return (left or right) and (up or down)
end

function complete ()

caterpillar:prepare( “aniCatipilar flying”)
caterpillar:addEventListener( “touch”, dragButterfly )

end
local function onTouch(event)
local t = event.target

local phase = event.phase
if “began” == phase then

leaf:play(“aniLeaf throb”)
caterpillar:play(“aniCatipilar moving”)
local parent = t.parent

display.getCurrentStage():setFocus( t )

t.isFocus = true

t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then

t.x = event.x - t.x0
t.y = event.y - t.y0

–print(hitTestObjects(box, catipilar))

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
caterpillar:play(“aniCatipilar still”)

if hitTestObjects(box, caterpillar) == true then
transition.to(leaf, { time=2500, alpha= 0, transition=easing.inOutExpo})
caterpillar:play( “aniCatipilar eating”)
leaf:play(“aniLeaf eat”)
box2.alpha = 1

end

if hitTestObjects(box2, caterpillar) == true then
print(“cocoon TIME!!!”)
caterpillar:play( “aniCatipilar Cocoon”)
caterpillar:removeEventListener( “touch”, onTouch )

local function touchcocoon (event)
–print (“touching cocoon YAY!”)

caterpillar:play( “aniCatipilar SliceCocoon”)

timer.performWithDelay ( 150, complete )

caterpillar:removeEventListener (“touch”, touchcocoon)

end
caterpillar:addEventListener (“touch”, touchcocoon)

end
end
end

return true
end

function dragButterfly(event)
local t = event.target

local phase = event.phase
if “began” == phase then

print("!!I am touching the Butterfly!!")
caterpillar:play(“aniCatipilar flying”)
local parent = t.parent

display.getCurrentStage():setFocus( t )

t.isFocus = true

t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then

t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

caterpillar:prepare(“aniCatipilar flying”)

end
end

return true
end
caterpillar:addEventListener( “touch”, onTouch )[/lua]

Thanks for any suggestions… [import]uid: 51459 topic_id: 22211 reply_id: 322211[/import]

Create a table for each type of object (caterpillar etc)
Create each object completely inside a function of its own and return the object from the function
Call the function in a loop and insert the returned object into it’s table [import]uid: 8271 topic_id: 22211 reply_id: 88433[/import]

Thanks @horacebury I will give that a try… [import]uid: 51459 topic_id: 22211 reply_id: 88439[/import]