{"id":174,"date":"2009-02-01T00:00:04","date_gmt":"2009-02-01T07:00:04","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=657"},"modified":"2009-02-01T00:00:04","modified_gmt":"2009-02-01T07:00:04","slug":"static-stage-and-more","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=174","title":{"rendered":"Static Stage and More&#8230;"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage {\r\n\timport flash.display.*;\r\n\r\n\tpublic class Main extends MovieClip{\r\n\t\r\n\t    private static var _display:MovieClip;\r\n\t    private static var _stage:Stage;\r\n\t    \r\n\t    \/\/ use getters instead of public static vars so they \r\n\t    \/\/ cannot be reset outside of Main\r\n\t    public static function get display():MovieClip{\r\n\t    \treturn _display;\r\n\t    }\r\n\t    \r\n\t    public static function get stage():Stage{\r\n\t    \treturn _stage;\r\n\t    }\r\n\t    \r\n\t\tpublic function Main(){\r\n\t\t   Main._display = this;\r\n\t\t   Main._stage = stage;\r\n\t\t   \r\n\t\t   \/\/ test out the static references\r\n\t\t   var t:Test = new Test();\r\n\t\t}\r\n\t}\r\n}\r\n\r\nclass Test{\r\n\tpublic function Test(){\r\n\t\t\/\/ test out the static references\r\n\t\twith(Main.display.graphics){\r\n\t\t\tlineStyle(1, 0xFF0000);\r\n\t\t\tfor (var i:int = 0; i&lt;100; i++){\r\n\t\t\t\tlineTo(Math.random() * Main.stage.stageWidth, \r\n\t\t\t\t       Math.random() * Main.stage.stageHeight);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>This snippet creates two private static variables that reference the stage and the main timeline\/document class. It then uses getters to regulate the use of these static vars so that they cannot be reset from outside the Main class. <\/p>\n<p>Sometimes the amount of extra coding you need to do to maintain a valid stage reference can be cumbersome&#8230; similarly, passing references of the document class all around your app can be annoying. If you don&#8217;t mind using two global vars in your app&#8230; this trick can come in handy. <\/p>\n<p>What&#8217;s nice about using getters here is that if someone tries to do this:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nMain.display = new MovieClip();\r\n<\/pre>\n<p>they&#8217;ll get an error&#8230; in flex, you even see that little red ex pop up next to this line of code if you write it \ud83d\ude42 &#8230; that wouldn&#8217;t happen with a public static var&#8230;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>package { import flash.display.*; public class Main extends MovieClip{ private static var _display:MovieClip; private static var _stage:Stage; \/\/ use getters instead of public static vars so they \/\/ cannot be reset outside of Main public static function get display():MovieClip{ return _display; } public static function get stage():Stage{ return _stage; } public function Main(){ Main._display = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,2,68],"tags":[3,4,120],"class_list":["post-174","post","type-post","status-publish","format-standard","hentry","category-displayobject","category-oop","category-variables","tag-actionscript","tag-flash","tag-flex"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=174"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/174\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}