PDA

View Full Version : Singleton Pattern



alexgeek
June 7th, 2009, 07:05 PM
Hi,
I have a movieclip called "GameArea" on my stage (instance name "gameArea") and have generated a class for it.
How can I incorporate the singleton pattern so that when I reference the GameArea class in other files, it gives me an instance that is on the stage?

Thanks, Alex.

Felixz
June 8th, 2009, 04:39 AM
http://www.google.com/search?hl=pl&rls=com.microsoft%3Apl%3AIE-SearchBox&q=as3+singleton&lr=

JonnyR
June 8th, 2009, 06:24 AM
alexgeek,

Another option would be to use a Locator / Multiton Pattern to retrieve your GameArea instance. This involves creating a GameAreaLocator class which comprises only of static members and properties:



public class GameAreaLocator
{
private static var INSTANCE : GameAreaLocator;

public static function setInstance(instance : GameArea) : void
{
INSTANCE = instance;
}

public static function getInstnace() : GameArea
{
if (INSTANCE is GameArea)
{
return INSTANCE;
}
else
{
throw new IllegalOperationError("GameArea instance must be set via GameAreaLocator.setInstance() before retrieval!");
return null;
}
}
}


Jonny.

alexgeek
June 8th, 2009, 07:49 AM
Thanks for the replies, helped a lot :)