Object Removal

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>

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). [import]uid: 52430 topic_id: 17074 reply_id: 64100[/import]

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? [import]uid: 89724 topic_id: 17074 reply_id: 64104[/import]

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

[lua]if maxHealth >= 100 then
wings:removeSelf()
wings = nil
end[/lua]

I realized that I should keep the healthRect bar for when the rocket is in flight…

Did you get the pictures I twitted? [import]uid: 51459 topic_id: 17074 reply_id: 64107[/import]

I got it!!
[lua]if maxHealth == wings.health then
wings:removeSelf()
wings = nil
end[/lua]

Thanks Jonathan Beebe! You are a genius! [import]uid: 51459 topic_id: 17074 reply_id: 64112[/import]

Glad to see that you got everything straightened out! [import]uid: 52430 topic_id: 17074 reply_id: 64130[/import]

I am glad that your glad! :slight_smile:

If you have another second, I have another question that relates to your ‘Martian Controls’ and the removal of event listeners.

I have a separate file which is modeled after your ‘Martian Controls’ and is essentially the next step in my game…

After the wings have been repaired the player must drag a path from the rocket ship to the planet and the rocket follows that path when the player has made the connection to the planet.

My confusion is how can I have 2 touch events, one that is connected to repairing the rocket and the other that sends the rocket home, work together so that when I touch the screen for the first step it does not create a line… And when the code reaches the second you can then see the line…

Thanks for any suggestion Jonathan Beebe!!

[import]uid: 51459 topic_id: 17074 reply_id: 64316[/import]

I also am planning on putting these two function into its own class since its going to be for multiple rocket ships that are different colors and need to go to different colored planets…

I plan on doing something like whats in your great blog post about OOP

http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/

Great job by the way… I really understand the power of classes!

I feel like I have the power of 1000 suns!! :slight_smile: [import]uid: 51459 topic_id: 17074 reply_id: 64319[/import]

Ignore that last ?.. I know what I need to do!

I CAN do something like if wings ARE not repaired then isVisbile = false and then set the isVisible to true when the wings are fixed!

I can’t wait till my game is ready to be in the apple market!! I think your really going to like it. I spent a lot of time trying to make it original, unique and FUN!!

CORONA SDK is the BEST!!! [import]uid: 51459 topic_id: 17074 reply_id: 64611[/import]

jonathan thanks! you’ve just saved me a lot of trouble :slight_smile: [import]uid: 108757 topic_id: 17074 reply_id: 85429[/import]