All I want to do is get rid of the heathRect and the wings when wings.healthRect reaches 100…
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 confermation 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 = 2
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 moveMe(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
–armyMan:play(“armyMan armyMan”)
elseif a_health > 40 then
healthRect:setFillColor(0,255,0)
–armyMan:play(“armyMan melting”)
elseif a_health > 20 then
healthRect:setFillColor(180,180,0)
–armyMan:play(“armyMan moreMelted”)
elseif a_health < 20 then
–armyMan:play(“armyMan plasticBall”)
healthRect:setFillColor(255,0,0)
–audio.play( scream )
end
if a_health > 1 then
healthRect:setReferencePoint(display.TopLeftReferencePoint)
healthRect.width = (a_health/5)
print(a_health)
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)
if wings.health > 100 then wings.health = 100 end
healthRect:removeSelf()
wings:removeSelf()
wings = nil
healthRect = nil
updateHealth()
else
healthRect.x = wings.x + (wings.contentWidth/2)
healthRect.y = wings.y - wings.contentHeight
end
end
local function removeMe( _t)
_t:removeSelf()
_t = nil
end
local function spawnRocks()
local rock = display.newImage(“rock.png”)
rock.x = _W + rock.contentWidth
rock.y = rand(1,480)
local scale = rand(3)
rock:scale(scale, scale)
rock.rotation = rand(360)
local moveSpeed = rand(1000, 3000)
– local moveSpeed = 3000 - (score / 50)
transition.to(rock, {time=moveSpeed, x=-50, onComplete=removeMe})
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”,moveMe)
Runtime:addEventListener(“enterFrame”,onEnterFrame)
timer.performWithDelay(1000, spawnRocks,0) [/lua]
You can see I already tried to do it but I get this nasty runtime error telling me that I am not allowed to do that!!
Runtime error
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/443/main.lua:79: attempt to index upvalue ‘wings’ (a nil value)
stack traceback:
[C]: ?
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/443/main.lua:79: in function ‘updateHealth’
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/443/main.lua:128: in function <…zs-a189gnqo1k>
?: in function <?:215>
Any help would be great!!
Thanks
[import]uid: 51459 topic_id: 17074 reply_id: 317074[/import] </…zs-a189gnqo1k>
