Wing Repair

This is a modified code from a tutorial I helped OzApps with, you can view it here http://howto.oz-apps.com/2011/10/little-plastic-armyman-and-fire.html#more

As you can see from the code and video posted that the army man toy has a health bar. If the army man is placed in the fire the army man melts and health goes down. If you click on the heal button the army man heals and gets back to normal when his health hits 100%

I like the idea behind the health bar but I don’t want it to start out green, I want it to start out small and red and grow to green and stop at 100% like the heal button.

I figured out that the healthRect controls what the how the health bar starts out as.
[lua]local healthRect = display.newRect(0,0,5,5)
healthRect:setFillColor(255,0,0)[/lua]

So NOW that it starts out red and small. I want it to grow and turn orange and then green and stop at 100% when in the “repair zone” Like the healMe button in the original armyMan tutorial.

I am not sure why my code is not working… It seems to stay red, jump to 100% and never turns green! I am very confused and any help would be much appreciated…

Here is my code:

[lua]

–[[

In this project the main objective I am trying to accomplish is to repair the wings
on a rocket ship.

By doing this and having visual conformation that the wings are repaired I will show a health bar
and a sparks from particle candy.

Code should show a small red bar which then grows orange then green

]]
display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

local i = 1
local maxHealth = 100
local rand = math.random
local speed = 4
local intensity = 5
local _W = display.contentWidth
local _H = display.contentHeight

local background = display.newImageRect(“background.png”, 1024, 768)
background.x = _W/2; background.y = _H/2

local rocketBody = display.newImageRect(“Body.png”, 26, 127)
rocketBody.x = 150; rocketBody.y = 650

local planet = display.newImageRect(“Planet.png”, 114, 114)
planet.x = 150; planet.y = 150
local healthRect = display.newRect(0,0,5,5)
healthRect:setFillColor(255,0,0)

local wings = display.newImageRect(“wings.png”, 50, 50)
wings.x = rand(100,550); wings.y = 150
wings.health = maxHealth
local intensityNo = display.newText("Intensity : " … intensity ,0,0,native.systemFontBold,32)
–intensityNo:scale(0.5,0.5)

intensityNo.y = 200
intensityNo.x = _W/2
local function repairWings(event)
local phase = event.phase
local x = event.x
local y = event.y
local target = event.target

if “began” == phase then
display.currentStage:setFocus(wings)
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)
wings.isFocus = false
target.x0 = nil
target.y0 = nil
end
end

local function updateHealth()
local a_health = math.floor(wings.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()
wings:removeSelf()
rocketBody:removeSelf()

wings=nil
end

end

local function onEnterFrame(event)
if wings==nil then
Runtime:removeEventListener(“enterFrame”, onEnterFrame)
return
end
if wings.isFocus==true then return end

healthRect.x = wings.x + (wings.contentWidth/2)
healthRect.y = wings.y - wings.contentHeight

if wings.y < (_H *.90) then
wings.y = wings.y+speed

–repairZone
elseif wings.x >140 and wings.x < 200 then
–wings.health = wings.health + (0.1 * intensity)
updateHealth()

else
healthRect.x = wings.x + (wings.contentWidth/2)
healthRect.y = wings.y - wings.contentHeight
end

end

local function changeIntensity(event)
intensity = intensity + 1
if intensity > 5 then intensity = 1 end
intensityNo. text = "Intensity : " … intensity
end

function healMe(event)
wings.health = math.floor(wings.health + 10)
if wings.health > 100 then wings.health = 100 end
updateHealth()
end

intensityNo:addEventListener(“tap”,changeIntensity)
wings:addEventListener(“touch”,repairWings)
Runtime:addEventListener(“enterFrame”,onEnterFrame) [/lua]

THANKS [import]uid: 51459 topic_id: 16861 reply_id: 316861[/import]