Following on from my previous post and my experiences with Lua & the Corona SDK, in the development of my current game I have created a simple manager class for transitioning screens when user input defines a screen change resulting in a needed transition. The class is very simple and implements the Singleton pattern I outlined in my previous post, the details are as below and as I have said it is very simple and does one thing only as all classes should - manages the transition of screens.
-
--
-
-- User: Shane Johnson
-
-- ScreenManager class to handle screen transitions
-
--
-
-
ScreenManager = {}
-
-
local _instance = {}
-
-
function ScreenManager:getInstance()
-
if not _instance then
-
_instance = ScreenManager
-
end
-
-
function removeScreen(screen)
-
if screen ~= nil then
-
screen:removeSelf()
-
end
-
end
-
-
_instance.changeScreenLeft = function(screenOne, screenTwo, clearScreen)
-
screenTwo.x = -screenTwo.width
-
screenTwo.alpha = 0
-
if clearScreen == true then
-
transition.to(screenOne, {
-
time = 300,
-
x = display.contentWidth,
-
alpha = 0,
-
onComplete = function()
-
removeScreen(screenOne)
-
end
-
})
-
else
-
transition.to(screenOne, { time = 300, alpha = 0, x = display.contentWidth })
-
end
-
transition.to(screenTwo, { time = 300, x = 0, alpha = 1 })
-
end
-
-
_instance.changeScreenRight = function(screenOne, screenTwo, clearScreen)
-
screenTwo.x = screenTwo.width
-
screenTwo.alpha = 0
-
if clearScreen == true then
-
transition.to(screenOne, {
-
time = 300,
-
x = display.contentWidth,
-
alpha = 0,
-
onComplete = function()
-
removeScreen(screenOne)
-
end
-
})
-
else
-
transition.to(screenOne, { time = 300, alpha = 0, x = -display.contentWidth })
-
end
-
transition.to(screenTwo, { time = 300, x = 0, alpha = 1 })
-
end
-
-
return _instance
-
end
-
-
function ScreenManager:new()
-
assert(nil, 'ScreenManager is a singleton and cannot be instantiated - use getInstance() instead')
-
end
In order to use it you would need to would need to handle the instatiation yourself and the tell the ScreenManager to handle the transition, so:
-
gameView = GameView:new()
-
ScreenManager:getInstance().changeScreenLeft(menuView, gameView)
The two change functions changeScreenLeft and changeScreenRight both do as the function name defines and also allow for a flag to be passed in to define whether the transitioning screen should be removed once the transition is finished.
Happy coding.

![Validate my RSS feed [Valid RSS]](http://www.ultravisual.co.uk/blog/images/valid-rss.png)
