Structuring my code

Dear all,

as before, I am learning Corona, and I’m quite impressed. I need, as a newbie, to learn how I can structure my code! It’s easy what I want: each file should draw a single screen, but I am not completely sure.

Let’s say I want to do something simple:

  • application loads a logo screen
  • after few seconds, it should transition to the “menu” screen, releasing memory
  • menu should handle inputs and call/transition to the relative screen, releasing memory

What have I done? Easy: I wrote few functions to run a module, and to clear its memory.

This is my main:
[lua]-- Load logo library
local logo = require(“logo”)
local menu = require(“menu”)

– Menu function
local function runMenu(event)
logo:clear()
timer.cancel(event.source)
menu:run()
end

– Init the logo
logo:run()

print(“Calling menu after 2 secs”)

– Call menu after few seconds
timer.performWithDelay(2000, runMenu)[/lua]

My logo source is quite easy: it creates a group, and adds a background and an image:
[lua]module(…, package.seeall)

print(“Defining logo vars”)

– Local variables
local image = display.newImage(“img/logo.png”, true)
local background = display.newRect(0, 0, display.contentWidth, display.contentHeight)

– Init screen
function logo:run()
print(“Logo runs”)
– Start a new screen
content = display.newGroup()
– Set the background
background:setFillColor( 136, 29, 35, 255 )
– Set logo image
image.xScale = display.contentWidth / (2 * image.contentWidth)
image.yScale = image.xScale
image.x = display.contentWidth / 2
image.y = display.contentHeight / 2
– Add contents
content:insert(background)
content:insert(image)
end

– Clear memory
function logo:clear()
print(“Logo clears”)
– Remove objects
for i = content.numChildren, 1, -1 do
local child = content[i]
print("Removing child object " … i … “:” … tostring(child))
child.parent:remove(child)
end
print("Removing object " … tostring(content))
content:removeSelf()
end[/lua]

This works, and so this is my menu:
[lua]module(…, package.seeall)

– Local variables
local image = display.newImage(“img/menu/background.png”, true)

– Start menu
function menu:run()
print(“Starting menu”)
content = display.newGroup()
– Set logo image
image.x = display.contentWidth / 2
image.y = display.contentHeight / 2
– Add contents
content:insert(image)
–transition.dissolve(logo:content(), content, 1000, 0)
end

– Clear memory
function menu:clear()
print(“Menu clears”)
– Remove objects
for i = content.numChildren, 1, -1 do
local child = content[i]
print("Removing child object " … i … “:” … tostring(child))
child.parent:remove(child)
end
print("Removing object " … tostring(content))
content:removeSelf()
end[/lua]

As you can see, the transition in my menu is commented because I don’t really know how to handle it. How can I say to menu that it must transition from the current display to menu:content?

Moreover, how can I make an event loop? Do I simply need to add buttons to menu, handlers for them to other screens, and next add a simple while infinite loop?

Thanks & Cheers?

PS. Yes, I’d like to structure my code as an object oriented one… am I doing it completely wrong? [import]uid: 94362 topic_id: 17382 reply_id: 317382[/import]