modular programming?

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 :slight_smile:

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]

Not sure how helpful this would be but i would do something like change this:

for i = #bars, 1, -1 do  
   
 --local bar = bars[i]  
 bar:update(planets[i].health,i)  
  
 end  
   
end  

to this:

for i = #bars, 1, -1 do  
   
 --update the table entry via the table, and then you can remove  
 --the id parameter from the update function, and change bar[id].isVisible..... to self.isVisible....  
 bars[i]:update(planets[i].health)  
  
 end  
   
end  

Sometimes I have found that tables do not list objects in the order you place them into the table. So you could end up with the table order being “bar2, bar3, bar1” even though the planet table is ordered “planet1, planet2, planet3”.
I can’t remember the exact details of why this happens I’m afraid. But if you get the bar to update self instead of updating a table entry which is possibly not the same as itself, then hopefully the correct planet’s health will be changed.

Hope this helps a bit.

[import]uid: 84115 topic_id: 19381 reply_id: 74894[/import]

You might find this tutorial very useful it includes the use of classes, subclasses and health bar.

http://jessewarden.com/2011/10/lua-classes-and-packages-in-corona.html

[import]uid: 38820 topic_id: 19381 reply_id: 74913[/import]

@alan120184: Thank you so much for that suggestion. I will try it and let you know. It does make sense. I am having hard getting my brain around this concept…which brings me to

@glennbjr: Thank you also for pointing me to this GREAT tutorial. That’s exactly what I needed. I could even use the code for my own as is!

Thank you guys. That is why I love this community. I lost some of hair the last few days (I am blaming my hair loss on Corona and I am sticking with that explanation!:slight_smile: but you guys always come to the rescue.

THANKS!

Mo [import]uid: 49236 topic_id: 19381 reply_id: 74955[/import]