Posted By

alsodenn on 11/12/10


Tagged

class main Singleton document as3


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

onemanstudio
Leang
tombarc
albertomori


Singleton


 / Published in: ActionScript 3
 

URL: http://stackoverflow.com/questions/370222/accessing-the-document-class-in-as3

You can use a singleton for your document class (Main, in your example), which allows you to access the instance from anywhere.

The document class is a pretty good candidate for the singleton pattern, because generally there should only be instance available.

  1. public class Main extends Sprite {
  2. private static var _instance:Main;
  3. public static function get instance():Main { return _instance; }
  4.  
  5. public function Main() {
  6. _instance = this;
  7. // etc...
  8. }
  9.  
  10. // etc...
  11. }
  12.  
  13. //Then you access the Main instance like this:
  14.  
  15. public class Other {
  16. public function Other() {
  17. Main.instance.usefulInstanceMethod();
  18. }
  19. }

Report this snippet  

You need to login to post a comment.