![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: as3, class, cs3, flash, singleton |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| In a lot of cases, you will only want one instance of an object living in your flash app (Singleton) - and here is a close-to-the-original implementation of that ![]() Code: package {
public class Controller extends Sprite {
private static var _instance:Controller;
private static var _allowInstantiation:Boolean;
public static function getInstance():Controller {
if(_instance == null) {
_allowInstantiation = true;
_instance = new Controller();
_allowInstantiation = false;
}
return _instance;
}
public function Controller():void {
if(!_allowInstantiation) {
throw new Error("Error: Instantiation failed: Use Controller.getInstance() instead of new.");
} else {
// Place all your code that you want to be hit only once here!
}
}
}
} __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes Last edited by ccoonen : 12-10-2007 at 07:41 AM. |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |