I have one box which has a one health meter that follows it and attached to a drag function. When this box is placed in the “repair zone” the health meter grow to green and the box turns green. 
[lua]local gameState = 0
local speed = 5
local _W = display.contentWidth
local _H = display.contentHeight
local speed = 4
local imgRect
local maxHealth = 100
local intensity = 5
local displayGroup = display.newGroup ( )
local rectTable = {}
function main ()
display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR
print(“game has started!!”)
end
–
–Display Objects
local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = _W/2; BG.y = _H/2
displayGroup:insert(BG)
local imgCir = display.newCircle(50,50,25,25)
imgCir.x = _W/2; imgCir.x = _H/2
displayGroup:insert(imgCir)
local healthRect = display.newRect(0,0,5,5)
healthRect:setFillColor(255,0,0)
displayGroup:insert(healthRect)
local imgRect = display.newRect(0,0,50,50)
imgRect.x = math.random(100, 500); imgRect.y = _W/2
imgRect.health = 2
displayGroup:insert(imgRect)
local repairZone = display.newRect(50,50,80,80)
repairZone.x = 200; repairZone.y = 300
repairZone:setFillColor(0,0,255)
repairZone.alpha = .3
displayGroup:insert(repairZone)
–
–Functions
local function onTouch(event)
local phase = event.phase
local x = event.x
local y = event.y
local target = event.target
if “began” == phase then
display.currentStage:setFocus(imgRect)
target.x0 = x
target.y0 = y
target.isFocus = true
elseif “moved” == phase and target.isFocus==true then
target.x = target.x + (x-target.x0)
target.y = target.y + (y-target.y0)
healthRect:setReferencePoint(display.TopLeftReferencePoint)
healthRect.x = target.x + (target.contentWidth/2)
healthRect.y = target.y - target.contentHeight
target.x0 = x
target.y0 = y
elseif “ended” == phase then
display.currentStage:setFocus(nil)
imgRect.isFocus = false
target.x0 = nil
target.y0 = nil
end
end
local function updateHealth()
local a_health = math.floor(imgRect.health,0)
if a_health > 60 then
elseif a_health > 40 then
healthRect:setFillColor(0,255,0)
elseif a_health > 20 then
healthRect:setFillColor(180,180,0)
elseif a_health < 20 then
healthRect:setFillColor(255,0,0)
end
if a_health > 1 then
healthRect:setReferencePoint(display.TopLeftReferencePoint)
healthRect.width = (a_health/5)
–print(a_health)
else
healthRect:removeSelf()
imgRect:removeSelf()
imgRect=nil
end
end
local function onEnterFrame(event)
if healthRect==nil then
imgRect:removeEventListener(“touch”,onTouch)
Runtime:removeEventListener(“enterFrame”, onEnterFrame)
return
end
if imgRect.isFocus==true then return end
healthRect.x = imgRect.x + (imgRect.contentWidth/2)
healthRect.y = imgRect.y - imgRect.contentHeight
if imgRect.y < (_H *.95) then
imgRect.y = imgRect.y+speed
elseif imgRect.x >186 and imgRect.x < 266 then
gameState = 2
imgRect.health = imgRect.health + (0.1 * intensity)
if imgRect.health > 100 then imgRect.health = 100 end
updateHealth()
if maxHealth == imgRect.health then
imgRect:setFillColor(0,255,0)
print(“Step 2 complete!”)
repairZone:removeSelf()
repairZone = nil
healthRect:removeSelf()
healthRect = nil
end
else
healthRect.x = imgRect.x + (imgRect.contentWidth/2)
healthRect.y = imgRect.y - imgRect.contentHeight
end
end
Runtime:addEventListener(“enterFrame”,onEnterFrame)
imgRect:addEventListener(“touch”,onTouch)
main ()[/lua]
What I would like to do is have multiple boxes with multiple health meters and the drag function attached to all of them.
I tried to put the boxes in a for loop like this one
[lua]for i=1,5 do
local imgRect = display.newRect(0,0,50,50)
imgRect.x = math.random(150,200) ; imgRect.y = math.random(150,350)
rectTable[i] = imgRect
rectTable[i].id = i
rectTable[i]:setFillColor(0,255,0)
rectTable[i].x = math.random(50,350)
rectTable[i].y = math.random(50,350)
rectTable[i]:addEventListener(“touch”, onTouch)
print(rectTable[i].id)
end[/lua]
But I got some errors… Any help with this would be AWESOME!!
Thanks… [import]uid: 51459 topic_id: 17247 reply_id: 317247[/import]

[import]uid: 51459 topic_id: 17247 reply_id: 65279[/import]