All I want to do is get rid of the heathRect and the wings when wings.healthRect reaches 100..
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | --[[ 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) |
This is because on line 128 you call updateHealth() (which is where your error is pointing to), but before you call updateHealth(), you remove wings and also set it to nil (which is what the error is complaining about).
UPDATE: To fix this, you should either use a different variable to store the health amount, or in your updateHealth() function, check to see if wings still exists before doing anything (it all depends on what exactly you're trying to accomplish).
I had a similar problem with tables throwing errors if one of the table's values was nil. It's detailed here:
http://developer.anscamobile.com/forum/2011/10/28/help-me-stab-tank
How do I get around that one?
Thanks Beebe! I want to remove those things because my rocket is actually a sprite loq file which I set to have no wings and then switch to having wings and also an animation of fire coming out of the bottom for when its in flight..
So as for your advice I have a maxHealth variable up thats = 100
can't I do something like this
1 2 3 4 | if maxHealth >= 100 then wings:removeSelf() wings = nil end |