Hello guys,
After seeing Jon posts about modular programing and the excelent “missile defense” tutorial (code sharing) I decided to do the right thing and converting my LONG code in gameScreen.lua into a couple modules (actually I am also trying to get away from module…seeall thing and use _M way. in any event here what I am trying to do:
1- display 3 planets images on the screen
2- for each of those planets, display a health bar
3- update those health bars as the planets get damaged (0-100%)
I am able to acheive 1 and 2 using the initPlanets() and initBars() with the code below. My problem is for step 3.
Basically I have 2 tables (planets and bars) and I instancied 3 planets and 3 bars on the screen. I then try to update the planets health bars during the game by calling the updateBars() function. The problem is when I do that, the system do not work as expected. The bars lenght changes when the planets are hit but most of the time the code changes the bar lenght of the wrong planet. I can see why. It seems that the update function do not seems to update the correct planet. What I cannot see is how to fix it!
I am not sure how to make sure the right barGRAPH is updated it. I can see that the initBars() put 3 different bar instance but I am not sure how to select the correct one (one that is connected to the related planet)
ANY pointers will be WELCOMED!
I can see why now the modular programing way is better. My gameScreen.lua is so much cleaner since I put planet and bars into "modules. It just seems that they are still couple things i need to learn 
THANKS!
Mo
ps: I am using the amazingly short Director class (15 lines!) which seems to work like a charm…
-------------------------- HERE A SHORT VERSION OF MY CODE -------------------
–gameScreen.lua
[lua]local _M = { }
function _M.new()
planets = { }
bars = { }
…
function initPlanets(n)
for i = 1, n do
local planet = Planet.new(i)
planets[#planets + 1] = planet
localGroup:insert(planet)
end
end
…
function initBars(n)
for i = 1, n do
local bar = Bar.new(planets[i])
bars[#bars + 1] = bar
print("bar#: "…i…“done”)
localGroup:insert(bar)
end
– game loop
– HERE BELOW THE PROBLEM I HAVE ( function updateBars() )!!!
–////////////////////////////////////////////////////////////////////////////////////////////////
function updateBar()
for i = #bars, 1, -1 do
–local bar = bars[i]
bar:update(planets[i].health,i)
end
end
…
– calling updateBar() every time the planets are hit in the collision event below
local function onRockCollision(self, event)
local p = event.other.id
if event.other.name == “planet” and event.other.health > 0 then
…
updateBar()
…
end
–/////////////////////////////////////////////////////////////////////////////////////////////////
– init game here
initVars()
initPlanets(3)
initBars(3)
end
end
return _M[/lua]
– planet.lua
[lua]local _M = { }
function Planet.new(i)
print(“making planets”)
assert(i, “Required parameter missing”)
local planet = display.newImageRect(imagesPath…“planet”…mRan(1,7)…".png",75,75 )
planet.x = mRand(0, 480)
planet.y = mRand(0, 320)
planet.health = 100
planet.rotation = mRand(360)
planet.rate = mRand(-1,1)
planet.id = i
planet.name = “planet”
physics.addBody( planet,“dynamic”,{radius=20,isSensor = true})
planet:applyTorque( -0.1*mRand(2) + 0.2 )
function planet:hit()
…
end
function planet:destroy()
…
end
return planet
end[/lua]
– bar.lua
[lua]local _M = { }
local Bar = { }
function Bar.new(planet)
print(“making bars”)
assert(p, “Required parameter missing”)
bar = display.newRect(planet.x - 25 , p.y - 30, 5 + planet.health*0.2,2)
bar:setFillColor(0,255,0,255)
bar.id = i
– HERE BELOW THE PROBLEM I HAVE
–////////////////////////////////
– red if health is < 20%
– yellow if planet health between 20 and 60%
– green line is planet health is > 60%
function bar:update(health, id)
if health <= 0 then
bar[id].isVisible = false
end
bar[id].xScale = 0.1 + health*0.01
if health >= 60 then bar[id]:setFillColor(0,255,0,255)
elseif health < 60 and health >= 20 then
bar[id]:setFillColor(255,255,0,255)
elseif health < 20 then
bar[id]:setFillColor(255,0,0,255)
end
end
return bar
end[/lua]
[import]uid: 49236 topic_id: 19381 reply_id: 319381[/import]