Corona SDK Screen manager class

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.

  1. --
  2. -- User: Shane Johnson
  3. -- ScreenManager class to handle screen transitions
  4. --
  5.  
  6. ScreenManager = {}
  7.  
  8. local _instance = {}
  9.  
  10. function ScreenManager:getInstance()
  11.     if not _instance then
  12.         _instance = ScreenManager
  13.     end
  14.  
  15.     function removeScreen(screen)
  16.         if screen ~= nil then
  17.             screen:removeSelf()
  18.         end
  19.     end
  20.  
  21.     _instance.changeScreenLeft = function(screenOne, screenTwo, clearScreen)
  22.             screenTwo.x = -screenTwo.width
  23.             screenTwo.alpha = 0
  24.             if clearScreen == true then
  25.                 transition.to(screenOne, {
  26.                     time = 300,
  27.                     x = display.contentWidth,
  28.                     alpha = 0,
  29.                     onComplete = function()
  30.                         removeScreen(screenOne)
  31.                     end
  32.                 })
  33.             else
  34.                 transition.to(screenOne, { time = 300, alpha = 0, x = display.contentWidth })
  35.             end
  36.             transition.to(screenTwo, { time = 300, x = 0, alpha = 1 })
  37.     end
  38.  
  39.     _instance.changeScreenRight = function(screenOne, screenTwo, clearScreen)
  40.             screenTwo.x = screenTwo.width
  41.             screenTwo.alpha = 0
  42.             if clearScreen == true then
  43.                 transition.to(screenOne, {
  44.                     time = 300,
  45.                     x = display.contentWidth,
  46.                     alpha = 0,
  47.                     onComplete = function()
  48.                         removeScreen(screenOne)
  49.                     end
  50.                 })
  51.             else
  52.                 transition.to(screenOne, { time = 300, alpha = 0, x = -display.contentWidth })
  53.             end
  54.             transition.to(screenTwo, { time = 300, x = 0, alpha = 1 })
  55.         end
  56.  
  57.     return _instance
  58. end
  59.  
  60. function ScreenManager:new()
  61.     assert(nil, 'ScreenManager is a singleton and cannot be instantiated - use getInstance() instead')
  62. 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:

[lua]
gameView = GameView:new()
ScreenManager:getInstance().changeScreenLeft(menuView, gameView)
[/lua]

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.

This entry was posted in Games, Lua and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>