Access denied. You must login to view this page.

Object Removal

9 replies [Last post]
jakescarano's picture
jakescarano
User offline. Last seen 4 weeks 3 days ago. Offline
Joined: 8 Apr 2011

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)      

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+++TI/-Tmp-/TemporaryItems/443/main.lua:105>
?: in function <?:215>

Any help would be great!!

Thanks

Replies

jonathanbeebe's picture
jonathanbeebe
User offline. Last seen 1 day 4 hours ago. Offline
Staff
Joined: 12 Apr 2011

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).

x2495iiii's picture
x2495iiii
User offline. Last seen 25 weeks 1 day ago. Offline
Joined: 9 Sep 2011

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?

jakescarano's picture
jakescarano
User offline. Last seen 4 weeks 3 days ago. Offline
Joined: 8 Apr 2011

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

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

Did you get the pictures I twitted?

jakescarano's picture
jakescarano
User offline. Last seen 4 weeks 3 days ago. Offline
Joined: 8 Apr 2011

I got it!!

1
2
3
4
if maxHealth == wings.health then
   wings:removeSelf()
        wings = nil
        end

Thanks Jonathan Beebe! You are a genius!

jonathanbeebe's picture
jonathanbeebe
User offline. Last seen 1 day 4 hours ago. Offline
Staff
Joined: 12 Apr 2011

Glad to see that you got everything straightened out!

jakescarano's picture
jakescarano
User offline. Last seen 4 weeks 3 days ago. Offline
Joined: 8 Apr 2011

I am glad that your glad! :)

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!!

jakescarano's picture
jakescarano
User offline. Last seen 4 weeks 3 days ago. Offline
Joined: 8 Apr 2011

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!! :)

jakescarano's picture
jakescarano
User offline. Last seen 4 weeks 3 days ago. Offline
Joined: 8 Apr 2011

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!!!!!

ggurak
User offline. Last seen 3 weeks 2 days ago. Offline
Joined: 3 Dec 2011

jonathan thanks! you've just saved me a lot of trouble :)

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.