Main - Air for Mobile


/ Published in: ActionScript 3
Save to your folder(s)

Often in my apps, some primary data are needed to be loaded, saved and then the App start to load the first page. Here is the best Main class I have so far come up with, that I use for my own apps.


Copy this code and paste it in your HTML
  1. package org.epayam.AppName {
  2. import com.greensock.events.LoaderEvent;
  3. import com.greensock.loading.LoaderMax;
  4. import com.greensock.loading.XMLLoader;
  5. import flash.display.Sprite;
  6. import flash.display.StageAlign;
  7. import flash.display.StageScaleMode;
  8. import flash.events.Event;
  9. import org.epayam.AppName.ast.Dta;
  10.  
  11. public class Main extends Sprite {
  12.  
  13. public static var p:Sprite; //path
  14.  
  15. private const initFileUrl:String = "xml/SomeFile.xml";
  16.  
  17. public function Main():void {
  18. if (stage) {
  19. init();
  20. } else {
  21. addEventListener(Event.ADDED_TO_STAGE, init);
  22. }
  23. }
  24.  
  25. /*~~~~~~~~~~~~~~~~~~~~~~~~(Private)~~~~~~~~~~~~~~~~~~~~~~~*/
  26. private function init(e:Event = null):void {
  27. removeEventListener(Event.ADDED_TO_STAGE, init);
  28. loadInitFiles();
  29. }
  30.  
  31. private function loadInitFiles():void {
  32. var loader:XMLLoader = new XMLLoader(initFileUrl, {name: "data",
  33. onComplete: dataLoaded});
  34. loader.load();
  35. }
  36.  
  37. private function dataLoaded(e:LoaderEvent):void {
  38. setData();
  39. setupApp();
  40. showApp();
  41. }
  42.  
  43. private function setData():void {
  44. Dta.pgeTxt = LoaderMax.getContent("data");
  45. }
  46.  
  47. private function setupApp():void {
  48. stage.scaleMode = StageScaleMode.NO_SCALE;
  49. stage.align = StageAlign.TOP_LEFT;
  50. App.p = this; // to be found by other classes
  51. }
  52.  
  53. private function showApp():void {
  54. app.init(); // an empty frame for loagding pages
  55. }
  56. }
  57. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.