AS3 Using a Static Class (Singleton)
In simple term – a static /singleton class
- Restricted instantiation to one object. Only one can be loaded in memory and shared by all classes that import it.
- Don’t have to instantiate it like other class. e.g. if the class is called GlobalStatic. You don’t have to do this, var myObject = new GlobalStatic(); We directly access it using GlobalStatic.methodName();
- Useful to pass data and share data globally.
Today, tutorial is just to introduce the concept of static / singleton in AS3 programming.
Before we start, let’s that a look at the actual demo script below. Notice, how the singleton work as a central memory, where you can “set” data to it and “get” data from it.
There’s actually 4 document classes written in this tutorial. Main.as which is the main class, GlobalStatic our singleton sample, mcRed and mcBlue. Before we start, is best you download the source file below here.
Main.as
package { import flash.display.MovieClip; import flash.events.Event; // Every class that import GlobalStatic can share the same method and // singleton class, you dont need instantiate it. // e.g. myGlobalStaticObject = new GlobalStatic(); // We just use it's name like GlobalStatic.methodName( ); import com.isgoodstuff.GlobalStatic; import flash.text.TextField; /************************************************************ * Description : This is the main actionscript file. * @author Alex T. ***********************************************************/ public class Main extends MovieClip { // Note : In this demo, mcGlobalStatic is just a visual representation of the GlobalStatic // as everything is stored in memory. public var txtBlueScore:TextField; public var txtRedScore:TextField; public function Main() { //Resets when the Main starts initializing txtRedScore.text = "0"; txtBlueScore.text = "0"; // Run all the time, to update the Main's fla txtBlueScore and txtRedScore // from GlobalStatic this.addEventListener(Event.ENTER_FRAME, handleUpdateScores); } private function handleUpdateScores(event):void { txtBlueScore.text = GlobalStatic.blueScore; txtRedScore.text = GlobalStatic.redScore; } } }
GlobalStatic.as
package com.isgoodstuff { import flash.display.MovieClip; import flash.text.TextField; /**************************************************************************** * Description : This is the mcGlobalStatic / Singleton Class * actionscript file. Is a good practice to place all your other class * files inside a sub folders, com means common libraries folder * isgoodstuff can be the company or organization. * @author Alex T. ***************************************************************************/ public class GlobalStatic extends MovieClip { private static var _iRedScore:int = 0; private static var _iBlueScore:int = 0; public function GlobalStatic() { // Very useful to cross reference and sending data across classes. // See how mcRed and mcBlue get data from each other using // globalStatic trace("Initializing globalStatic / Singleton Class"); _iRedScore = 0; _iBlueScore = 0; } // This is how to make a setter. So that other class can just use // GlobalStatic.redScore = value; to assign latest redScore public static function set redScore(value:int) { _iRedScore = value; } // This is how to make a getter. So that other class can just use // var myRedScore = GlobalStatic.redScore to get latest redScore public static function get redScore() { return _iRedScore; } // This is how to make a setter. So that other class can just use // GlobalStatic.blueScore = value; to assign latest blueScore public static function set blueScore(value:int) { _iBlueScore = value; } // This is how to make a getter. So that other class can just use // var myBlueScore = GlobalStatic.blueScore to get latest blueScore public static function get blueScore() { return _iBlueScore; } } }
mcRed.as
package com.isgoodstuff { import flash.display.MovieClip; import flash.events.MouseEvent; import flash.text.TextField; // To get and receive value from GlobalStatic, we must first include it into our import import com.isgoodstuff.GlobalStatic; /************************************************************ * Description : This is the mcRed Class actionscript file. * It is binded to the movieClip in Library named mcRed. * @author Alex T. ***********************************************************/ public class mcRed extends MovieClip { // Instance placed in stage must be declared public var before // they can be used. public var txtRedScore : TextField; public var txtBlueScore : TextField; public var btnSet:MovieClip; public var btnGet:MovieClip; public function mcRed() { //Reseting own numbers in own textfield. txtBlueScore.text = "0"; txtRedScore.text = "0"; btnGet.addEventListener(MouseEvent.CLICK, handleGetClick ); btnSet.addEventListener(MouseEvent.CLICK, handleSetClick ) ; } private function handleGetClick(event) :void { txtBlueScore.text = String(GlobalStatic.blueScore); } private function handleSetClick(event):void { GlobalStatic.redScore = parseInt(txtRedScore.text); } } }
mcBlue.as
package com.isgoodstuff { import flash.display.MovieClip; import flash.events.MouseEvent; import flash.text.TextField; // To get and receive value from GlobalStatic, we must first include it into our import import com.isgoodstuff.GlobalStatic; /************************************************************ * Description : This is the mcBlue Class actionscript file. *It is binded to the movieClip in Library named mcBlue. * @author Alex T. ***********************************************************/ public class mcBlue extends MovieClip { // Instance placed in stage must be declared public var before // they can be used. public var txtRedScore : TextField; public var txtBlueScore : TextField; public var btnSet:MovieClip; public var btnGet:MovieClip; public function mcBlue() { //Reseting own numbers in own textfield. txtBlueScore.text = "0"; txtRedScore.text = "0"; btnGet.addEventListener(MouseEvent.CLICK, handleGetClick ); btnSet.addEventListener(MouseEvent.CLICK, handleSetClick ) ; } private function handleGetClick(event) :void { txtRedScore.text = String(GlobalStatic.redScore); } private function handleSetClick(event):void { GlobalStatic.blueScore = parseInt(txtBlueScore.text); } } }
I hope in this tutorial, will give you insight of how singleton is use in daily oop programming. I personally like to use them as reusable utility classes like calculateDistance, findAngle for game programming and etc. Sometimes, movieClip instance can be passed in as well so that, the other class can access it properties.





Excellent post. You must continue to offer excellent resources and content like you have been offering. I will most likely stop by again in the future.
bal: IIRC, singletons are a design pattern (a way of creating classes that fits in most projects). There are a lot of different implementations but the point is always the same: having ONLY ONE instance of a class.
Magnificent! (As usual.
)
Its surely the wrong source file ?
oh dear sorry Paul, my bad, I override the wrong file. Updated upload today