Posted By

MichaelJWilliams on 04/13/09


Tagged

system media interface ui gamescene riverman scene scenes screens gamedev


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Pingpongbal
zoo


Simplify Flow Between Screens with the “Scene System”


 / Published in: ActionScript 3
 

URL: http://gamedev.michaeljameswilliams.com/2009/04/13/as3-scene-system/

This pair of classes will help you simplify the flow between the various screens of your game or app. Check my blog for full details and instructions.

  1. package com.michaeljameswilliams.gamedev.gamescenes
  2. {
  3.  
  4. /**
  5. * Based on http://www.rivermanmedia.com/programming/6-object-oriented-game-programming-the-scene-system
  6. * More info: http://gamedev.michaeljameswilliams.com/2009/04/13/as3-scene-system/
  7. * @author MichaelJWilliams
  8. */
  9.  
  10. public interface IGameScene
  11. {
  12. /**
  13. * Update logic for this game scene.
  14. */
  15. function update():void;
  16. /**
  17. * Redraw everything on screen.
  18. */
  19. function redraw():void;
  20. /**
  21. * Load all of the data and graphics that this scene needs to function.
  22. */
  23. function load():void;
  24. /**
  25. * Unload everything that the garbage collector won't unload itself, including graphics.
  26. */
  27. function unload():void;
  28. }
  29.  
  30. }
  31.  
  32.  
  33.  
  34.  
  35.  
  36. package com.michaeljameswilliams.gamedev.gamescenes
  37. {
  38. import flash.display.DisplayObject;
  39. import flash.display.DisplayObjectContainer;
  40.  
  41. /**
  42. * Based on http://www.rivermanmedia.com/programming/6-object-oriented-game-programming-the-scene-system
  43. * More info: http://gamedev.michaeljameswilliams.com/2009/04/13/as3-scene-system/
  44. * Singleton template from here: http://www.gskinner.com/blog/archives/2006/07/as3_singletons.html
  45. * @author MichaelJWilliams
  46. */
  47.  
  48. public class GameSceneManager {
  49. private static var _instance:GameSceneManager;
  50. private static var _allowInstantiation:Boolean;
  51. private static var _currentScene:IGameScene;
  52. private static var _sceneContainer:DisplayObjectContainer;
  53.  
  54. public static function getInstance( p_sceneContainer:DisplayObjectContainer = null ):GameSceneManager
  55. {
  56. if ( _instance == null )
  57. {
  58. _allowInstantiation = true;
  59. _instance = new GameSceneManager();
  60. _allowInstantiation = false;
  61. }
  62.  
  63. if ( p_sceneContainer )
  64. {
  65. setSceneContainer( p_sceneContainer );
  66. }
  67.  
  68. return _instance;
  69. }
  70.  
  71. /**
  72. *
  73. * @param p_sceneContainer Object that is parent of each scene -- for example, the document class
  74. */
  75. public function GameSceneManager():void
  76. {
  77. if ( !_allowInstantiation )
  78. {
  79. throw new Error("Error: Instantiation failed: Use GameSceneManager.getInstance() instead of new.");
  80. }
  81. }
  82.  
  83. public static function setSceneContainer( p_sceneContainer:DisplayObjectContainer ):void
  84. {
  85. _sceneContainer = p_sceneContainer;
  86. }
  87.  
  88. /**
  89. * Update logic for the current scene.
  90. */
  91. public static function update():void
  92. {
  93. _currentScene.update();
  94. }
  95.  
  96. /**
  97. * Redraw everything on screen.
  98. */
  99. public static function redraw():void
  100. {
  101. _currentScene.redraw();
  102. }
  103.  
  104. /**
  105. * A simplified way of loading and unloading scenes
  106. * Notice that the method is static so that anyone can call it.
  107. * @param p_newScene The scene to switch to.
  108. */
  109. public static function changeScene( p_newScene:IGameScene ):void
  110. {
  111. if ( ( p_newScene is DisplayObject ) && ( _sceneContainer ) )
  112. {
  113. if ( _currentScene != null )
  114. {
  115. _sceneContainer.removeChild( _currentScene as DisplayObject );
  116. _currentScene.unload();
  117. }
  118. p_newScene.load();
  119. _currentScene = p_newScene;
  120. _sceneContainer.addChild( _currentScene as DisplayObject );
  121. }
  122. else
  123. {
  124. throw new Error("Error: game scenes must be of type DisplayObject and scene container must be set.");
  125. }
  126. }
  127. }
  128. }

Report this snippet  

You need to login to post a comment.