code check

Hi

I have just started using the corona sdk and so far I really like what I’ve seen :).
I am a web developer and have been interested in developing a game for a long time. I have started putting a little scene manager together, I am new to lua and would like to know if I am heading in the right direction.

the idea is to have scenes with standard functions i.e. load,update,unload maybe listeners but haven’t worked out how that would work. then to add each scene to the scene manager so I can switch from one scene to another and handle the cleaning up. is this over kill?

here is my code

[lua]-- sceneMgr.lua

sceneMgr = {}
sceneMgr.__index = sceneMgr

function sceneMgr.create()
local sm = {}
setmetatable(sm,sceneMgr) – make scene handle lookup
sm.currentScene = 1
sm.scenes = {}
sm.debugtxt = display.newText( “sceneManager Loaded”, 0, 0, native.systemFont, 10 )
sm.tPrevious = system.getTimer()
sm.running = false
return sm
end

function sceneMgr:changeScene(newScene)
self.currentScene = newScene
end

function sceneMgr:addScene(scene)
self.debugtxt.text = ‘sceen added’
table.insert(self.scenes,scene)
end

function sceneMgr:load()

self.debugtxt.text = 'loaded called has ’ … #self.scenes

self.debugtxt:setTextColor( 255,0,0 )

if self.scenes[self.currentScene] ~= nil then
self.debugtxt.text = ‘scene found’
self.scenes[self.currentScene]:load()
else
self.debugtxt.text = ‘current scene is nil’
end

end

function sceneMgr:start()
local _update = function(event)
self:update(event)
end
self.debugtxt:setTextColor( 0,0,0 )
Runtime:addEventListener(“enterFrame”,_update)
end

function sceneMgr:update(event)

local tDelta = event.time - self.tPrevious

self.tPrevious = event.time

if self.scenes[self.currentScene] ~= nil then
self.scenes[self.currentScene]:update(tDelta)
end

end

function sceneMgr:unload()

– call unload on current scene

end

function sceneMgr:loadSelected(sceneIndex)

end

function sceneMgr:updateSelected(sceneIndex)

end

function sceneMgr:unloadSelected(sceneIndex)

end

– main.lua

local menu = {}

local mu = menu.create()

local sm = sceneMgr.create()

sm:addScene(mul)
sm:load()
sm:start()[/lua] [import]uid: 12378 topic_id: 4976 reply_id: 304976[/import]

Welcome to Corona. I wouldn’t really say it’s overkill; I’m not bothering with a separate scene manager myself and simply managing my scenes in main.lua directly, but a lot of people use the Director class in the tutorials and that class is similar to what you are programming. Regardless of how exactly you do it, the vast majority of games should have some sort of concept of scene switching with different logic in different scenes, so that you can have separate scenes/logic for the main menu and in-game.

Is that your only question, is this methodology overkill? If you want to see how I organize my objects and scenes then check out this sample, but just glancing over it your approach looks fine:
http://developer.anscamobile.com/forum/2010/12/31/object-oriented-sample-game-framework-corona [import]uid: 12108 topic_id: 4976 reply_id: 16126[/import]

my question is probably overkill :slight_smile:
I just want to make sure I was heading the the right direction.
I’ve downloaded your example and will be checking it out tonight.

thanks jhock really appreciated

[import]uid: 12378 topic_id: 4976 reply_id: 16131[/import]

Coming up with a framework yourself is a great idea, don’t worry about overkill. You’ll get an invaluable insight into the nuts and bolts of how Corona/Lua work AND you’ll have a framework that does what you need it to.

Do as jhocking suggested and check out as much code as you can just to get ideas and see different implementations.

Get it to a working stage and then try to build some simple demos with it. Add events, timers, memory management, and other bits and bobs and you’ll quickly find out where the deficiencies lie.

I had four total rewrites of my framework before I was happy with it - so it wasn’t an easy task but definitely worth the effort in the end.

Good luck! [import]uid: 11393 topic_id: 4976 reply_id: 16132[/import]

Welcome to Corona!

Just so you know, there’s an excellent scene manager submitted by a Corona user a while back:

http://developer.anscamobile.com/code/director-class-10

You’re probably doing what you’re doing to teach yourself how it all works, and that’s awesome. I just wanted to let you know it’s out there if you want it.

Best of luck!

EDIT: Oops, I see my comments are redundant. Note to self: must read more carefully! :slight_smile:
[import]uid: 9659 topic_id: 4976 reply_id: 16622[/import]

Hi all

I got the first version of my scene manager up and running over the weekend. just want to say thanks you guys really help me out.

its still got a couple of bugs in it like the up and down changeScene seems to be messed up but I’m sure I’ll be able to fix that at some point.

if you’ve got any suggestion or I’ve made any lua related faux pas’s please let me know.

thanks again :slight_smile:

[lua]-- sceneManager
module(…, package.seeall)

function create(sm)

local self = {}
self.currentScene = 1
self.scenes = {}
self.tPrevious = system.getTimer()
self.pause = false

function self:changeScene(newScene)
self.currentScene = newScene
end

function self:addScene(scene)
table.insert(self.scenes,scene)
end

function self:load()

if self.scenes[self.currentScene] ~= nil then

self.scenes[self.currentScene]:load(self)

end

end

function self:start()

local _update = function(event)

self:update(event)

end

Runtime:addEventListener(“enterFrame”,_update)

end

function self:update(event)

if self.pause == false then

local tDelta = event.time - self.tPrevious

self.tPrevious = event.time

if self.scenes[self.currentScene] ~= nil then

self.scenes[self.currentScene]:update(self,tDelta)

end

end

end

function self:unload()

if self.scenes[self.currentScene] ~= nil then

self.scenes[self.currentScene]:unload()

end

end

function self:loadSelected(sceneIndex)

if self.scenes[sceneIndex] ~= nil then

self.scenes[sceneIndex]:load(self)

end

end

function self:updateSelected(sceneIndex)

end

function self:unloadSelected(sceneIndex)

end

function self:slideIn(direction,scene)

local xori = 0

local yori = 0

if direction == ‘up’ or direction == ‘down’ then

xori = self.scenes[scene].scene.x

end

if direction == ‘down’ then

yori = -self.scenes[scene].scene.height

end
if direction == ‘up’ then

yori = display.contentHeight + self.scenes[scene].scene.height

end

if direction == ‘left’ or direction == ‘rigth’ then

yori = self.scenes[scene].scene.y

end

if direction == ‘left’ then

xori = -self.scenes[scene].scene.width

end

if direction == ‘right’ then

xori = display.contentWidth + self.scenes[scene].scene.width

end

self.scenes[scene].scene.x = xori

self.scenes[scene].scene.y = yori

local _unload = function()

self:unload(self)

end

transition.to(self.scenes[scene].scene, {time = 200, x = 0, y = 0})

end

function self:slideOut(direction,scene)

local xori = 0

local yori = 0

if direction == ‘up’ or direction == ‘down’ then

xori = self.scenes[scene].scene.x

end

if direction == ‘down’ then

yori = -self.scenes[scene].scene.height

end

if direction == ‘up’ then

yori = display.contentHeight + self.scenes[scene].scene.height

end

if direction == ‘left’ or direction == ‘rigth’ then

yori = self.scenes[scene].scene.y

end

if direction == ‘left’ then

xori = display.contentWidth + self.scenes[scene].scene.width

end

if direction == ‘right’ then

xori = -self.scenes[scene].scene.width

end

local _onComplete = function()

self:unloadSelected(self)

end

transition.to(self.scenes[scene].scene, {time = 200, x = xori, y = yori})

end

function self:changeScene(direction,newscene)
self.pause = true
self:slideOut(direction,self.currentScene)
self.currentScene = newscene
self:loadSelected(newscene)
self:slideIn(direction,newscene)
self.pause = false
end

return self

end[/lua]

[import]uid: 12378 topic_id: 4976 reply_id: 16911[/import]