functions and object x 3

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. :slight_smile:

[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]

I FIGURED OUT A SOLUTION!! [import]uid: 51459 topic_id: 17247 reply_id: 65245[/import]

well then, post your solution) [import]uid: 16142 topic_id: 17247 reply_id: 65253[/import]

Hey darkconsoles! Welcome to my post!.. :slight_smile:

Well, I kind of figured out a solution… Still kind of stumped

I started to put things in tables… I was able to connect the functions to one square as you can see here

[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 i

local rectTable = {

{x=80,y=100},
{x=280,y=100},
{x=450,y=100}

}
local hitTable = {

{x=50,y=300},
{x=250,y=300},
{x=420,y=300}

}

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,1,5)
healthRect:setFillColor(255,0,0)
displayGroup:insert(healthRect)



–objects that need to be repaired


for i=1,3 do

local imgRect = display.newRect(0,0,50,50)
imgRect.x = rectTable[i].x ; imgRect.y = rectTable[i].y
rectTable[i] = imgRect
rectTable[i].id = i
rectTable[i]:setFillColor(0,0,150)
rectTable[i].health = 2
print(rectTable[i].id)
end



–hitTestObjects


for i=1,3 do

local hitArea = display.newRect(0,0,50,50)
hitArea.x = hitTable[i].x ; hitArea.y = hitTable[i].y
hitTable[i] = hitArea
hitTable[i].id = i
hitTable[i]:setFillColor(0,255,0)
hitTable[i].alpha = .3
print(hitTable[i].id)
end

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



–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(rectTable[1])
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)
rectTable[1].isFocus = false
target.x0 = nil
target.y0 = nil
end
end

local function updateHealth()
local a_health = math.floor(rectTable[1].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()
rectTable[1]:removeSelf()
rectTable[1]=nil
end
end

local function onEnterFrame(event)

if healthRect==nil then
rectTable[1]:removeEventListener(“touch”,onTouch)
Runtime:removeEventListener(“enterFrame”, onEnterFrame)
return
end
if rectTable[1].isFocus==true then return end

healthRect.x = rectTable[1].x + (rectTable[1].contentWidth/2)
healthRect.y = rectTable[1].y - rectTable[1].contentHeight

if rectTable[1].y < (_H *.95) then
rectTable[1].y = rectTable[1].y+speed

elseif hitTestObjects(rectTable[1], hitTable[1]) == true then
gameState = 1
rectTable[1].health = rectTable[1].health + (0.1 * intensity)

if rectTable[1].health > 100 then rectTable[1].health = 100 end
updateHealth()
if maxHealth == rectTable[1].health then
hitTable[1].alpha = 0
rectTable[1]:setFillColor(0,255,0)
print(“Step 1 complete!”)
healthRect:removeSelf()
healthRect = nil
end

else
healthRect.x = rectTable[1].x + (rectTable[1].contentWidth/2)
healthRect.y = rectTable[1].y - rectTable[1].contentHeight

end

end

Runtime:addEventListener(“enterFrame”,onEnterFrame)
rectTable[1]:addEventListener(“touch”,onTouch)

main ()[/lua]

Do you have any suggestion on how I can add the other two without a whole lot of code?

:slight_smile: [import]uid: 51459 topic_id: 17247 reply_id: 65259[/import]

if you need to drag objects around and each with its own health bar, then i’d do it like that
its taken some time for me to figure it out, but not much)

[lua]local enemyTable = {}

local function touch (event)
if event.phase == “began” then
event.target.isFocus = true
elseif event.phase == “moved” and event.target.isFocus == true then
event.target.x = event.x
event.target.y = event.y
event.target.bar.x = event.target.x - 20
event.target.bar.y = event.target.y - 30
elseif event.phase == “ended” or event.phase == “cancelled” then
event.target.isFocus = false
end
end

for i=1,3 do

enemyTable[i] = display.newRect(0,0,30,30)
enemyTable[i].x = math.random(50,300)
enemyTable[i].y = math.random(50,300)
enemyTable[i].health = 100
healthbar = display.newRect(0,0,40,4)
healthbar:setFillColor(255,0,0)
healthbar:setReferencePoint(display.TopLeftReferencePoint)
healthbar.x = enemyTable[i].x - 20
healthbar.y = enemyTable[i].y - 30
enemyTable[i].bar = healthbar
enemyTable[i]:addEventListener(“touch”, touch)

end[/lua] [import]uid: 16142 topic_id: 17247 reply_id: 65271[/import]

That looks interesting… Storing everything in one for loop! Why didn’t I think of that!.

When you put .bar is that like in my code when I put .health?

Also in my code the imgRect’s are not enemies but broken wings of ships that need to be repaired…

The point of this exercise is to experiment with the idea of having 3 different hitAreas that control the repairing *zone* and 3 different wing types that only get repaired if they are in the right repairing *zone*.

Does that make sense?

I think I know how I can do that… I’m going to do some more experimenting and get back to you… :slight_smile:

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

zones and repairs makes sense and its should be easy to accomplish, you just need to check every items .id or something like that [import]uid: 16142 topic_id: 17247 reply_id: 65276[/import]

Wow nice new pic! this kind of worked… But not exactly.
[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 i

local rectTable = {

{x=80,y=100},
{x=280,y=100},
{x=450,y=100}

}
local hitTable = {

{x=50,y=300},
{x=250,y=300},
{x=420,y=300}

}

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,1,5)
healthRect:setFillColor(255,0,0)
displayGroup:insert(healthRect)



–hitTestObjects


for i=1,3 do

local hitArea = display.newRect(0,0,50,50)
hitArea.x = hitTable[i].x ; hitArea.y = hitTable[i].y
hitTable[i] = hitArea
hitTable[i].id = i
hitTable[i]:setFillColor(0,255,0)
hitTable[i].alpha = .3
print(hitTable[i].id)
end

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



–Drag Function


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(rectTable[1])
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)
rectTable[1].isFocus = false
target.x0 = nil
target.y0 = nil
end
end



–objects that need to be repaired


for i=1,3 do

local imgRect = display.newRect(0,0,50,50)
imgRect.x = rectTable[i].x ; imgRect.y = rectTable[i].y
rectTable[i] = imgRect
rectTable[i].id = i
rectTable[i]:setFillColor(0,0,150)
rectTable[i].health = 2
print(rectTable[i].id)
rectTable[i]:addEventListener(“touch”,onTouch)

end



–Heal Function


local function updateHealth()
local a_health = math.floor(rectTable[1].health,0)
local a_health = math.floor(rectTable[2].health,0)
local a_health = math.floor(rectTable[3].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()
rectTable[1]:removeSelf()
rectTable[1]=nil
rectTable[2]:removeSelf()
rectTable[2]=nil
rectTable[3]:removeSelf()
rectTable[3]=nil
end
end

local function onEnterFrame(event)

if healthRect==nil then
rectTable[1]:removeEventListener(“touch”,onTouch)
rectTable[2]:removeEventListener(“touch”,onTouch)
rectTable[3]:removeEventListener(“touch”,onTouch)
Runtime:removeEventListener(“enterFrame”, onEnterFrame)
return
end

if rectTable[1].isFocus==true then return end
if rectTable[2].isFocus==true then return end
if rectTable[3].isFocus==true then return end

healthRect.x = rectTable[1].x + (rectTable[1].contentWidth/2)
healthRect.y = rectTable[1].y - rectTable[1].contentHeight

healthRect.x = rectTable[2].x + (rectTable[1].contentWidth/2)
healthRect.y = rectTable[2].y - rectTable[1].contentHeight

healthRect.x = rectTable[3].x + (rectTable[1].contentWidth/2)
healthRect.y = rectTable[3].y - rectTable[1].contentHeight

if rectTable[1].y < (_H *.95) then
rectTable[1].y = rectTable[1].y+speed

if rectTable[2].y < (_H *.95) then
rectTable[2].y = rectTable[2].y+speed

if rectTable[3].y < (_H *.95) then
rectTable[3].y = rectTable[3].y+speed
end
end

elseif hitTestObjects(rectTable[1], hitTable[1]) == true and hitTestObjects(rectTable[2], hitTable[2]) == true and hitTestObjects(rectTable[3], hitTable[3]) == true then
gameState = 1
rectTable[1].health = rectTable[1].health + (0.1 * intensity)
rectTable[2].health = rectTable[2].health + (0.1 * intensity)
rectTable[3].health = rectTable[3].health + (0.1 * intensity)

if rectTable[1].health > 100 then rectTable[1].health = 100 end
if rectTable[2].health > 100 then rectTable[2].health = 100 end
if rectTable[3].health > 100 then rectTable[3].health = 100 end
updateHealth()
if maxHealth == rectTable[1].health then
hitTable[1].alpha = 0
rectTable[1]:setFillColor(0,255,0)
print(“ship 1 repaired!”)
healthRect:removeSelf()
healthRect = nil
end

if maxHealth == rectTable[2].health then
hitTable[2].alpha = 0
rectTable[2]:setFillColor(0,255,0)
print(“ship 2 repaired!”)
healthRect:removeSelf()
healthRect = nil
end

if maxHealth == rectTable[3].health then
hitTable[3].alpha = 0
rectTable[3]:setFillColor(0,255,0)
print(“ship 3 repaired!”)
healthRect:removeSelf()
healthRect = nil
end
else
healthRect.x = rectTable[1].x + (rectTable[1].contentWidth/2)
healthRect.y = rectTable[1].y - rectTable[1].contentHeight

healthRect.x = rectTable[2].x + (rectTable[2].contentWidth/2)
healthRect.y = rectTable[2].y - rectTable[2].contentHeight

healthRect.x = rectTable[3].x + (rectTable[3].contentWidth/2)
healthRect.y = rectTable[3].y - rectTable[3].contentHeight

end

end

Runtime:addEventListener(“enterFrame”,onEnterFrame)

main ()[/lua]

The healthRect is not showing up on all the “wings”

and I got this error because I am removing the healthRect when the wings have been repaired…

Runtime error
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/482/main.lua:228: attempt to index upvalue ‘healthRect’ (a nil value)
stack traceback:
[C]: ?
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/482/main.lua:228: in function <…zs-a189gnqo1k>
?: in function <?:215>
[import]uid: 51459 topic_id: 17247 reply_id: 65277[/import] </…zs-a189gnqo1k>

OH I see why the healthRect is not on all the “wings” [import]uid: 51459 topic_id: 17247 reply_id: 65278[/import]

never mind I thought I saw it… :frowning:

Do you know a way to simplify what I just gave you?

To much code gives me a head ache… :confused: [import]uid: 51459 topic_id: 17247 reply_id: 65279[/import]

so, you need three different parts, every part is with self health bar
and you need to drag those parts in corresponding repair zone and it will be repaired only if its correct match of part\zone

if its like that, then its not so hard than you think)

and if you think that 200 lines is much of a code, than i have to tell you that 200 lines is nothing)

you know, there’s no need to spamming forums like this, we can chat through icq or skype, it will be easier for you and me [import]uid: 16142 topic_id: 17247 reply_id: 65285[/import]

Sure Im up for talking on Skype!! Its better for me because then I can talk it out with a human instead of talking to a computer… :slight_smile: Whats you’re Skype name? mines easy jakescarano… I’ll sign on right now. [import]uid: 51459 topic_id: 17247 reply_id: 65286[/import]