I`ve just tried out the AS3 singelton (instead of AS2_global) var in my project and it does not work. I simply can not understand it .It seems to me I must be crazy or I am not getting something right.
Everybody needs to load SWF files on top of others ( what was called _level in AS2) , right ? Otherwise you simply can not build a flash site. It would be way to heavy, right ?
Everyone need Global variables , right ?
Everyone uses them, right ?
Otherwise how could you keep track of values when you move from one Level ( or in AS3 display Object ) to another ?
So how come there is no clear cut way to use global variables in between SWF files who are in the same display list ?
If this is impossible it would be as if Adobe just cut away the whole brunch of flash sites...
They would`nt do such a silly thing, that`s why I`m sure I`m not getting here something...
It`s such a simple request. I do not complain that it is no longer as simple as `_global.Variable = value` as it was in AS2 , I understand that things should be more structured in AS3, but it should at least be possible to achieve , no ?
I`ve tried to check the lead you gave me, Moonkey, about LocalConnection and from what I could gather this is meant for communication in between SWF files playing on Different Players.
Mine is not such a case, I only have a simple display list with various SWF files loaded into it, exactly like you do with different MovieClips( only they are SWF files), there MUST be a simple way to share a global variable in between them.
I attach here the singelton that I copied from The link PixelWelder gave me. I put into it a variable called Language which is the variable that I want to be the global variable to share among all of them just like _global was in AS2, but each SWF keeps a different value of this Language var. here is the singleton code:
package {
public class Global {
private static var _instance:Global;
public var Language:String;
public static function get instance() {
if ( !_instance ) {
_instance = new Global();
}
return _instance;
}
public function Global() {
if ( _instance ) {
throw new Error("Singleton already exists- use get instance() to access");
} else {
trace( "new Global!" );
}
}
}
}
And here is the code for a 3 button set assigning values and calling that `Language` global variable. it works when it`s located in the same SWF, but once I have this 3 button set duplicated to various SWF files all added into the same display list - they all have a different value of `Language`:
import Global;
aYes.addEventListener(MouseEvent.CLICK , Yes);
function Yes(evt:MouseEvent):void {
trace("a.swf Sets to Yes");
Global.instance.Language = "Yes";
}
aNo.addEventListener(MouseEvent.CLICK , No);
function No(evt:MouseEvent):void {
trace("a.swf Sets to No");
Global.instance.Language = "No";
}
aShow.addEventListener(MouseEvent.CLICK , Show);
function Show(evt:MouseEvent):void {
trace("a.swf shows: "+Global.instance.Language);
}
I also uploaded that whole little project here, so that you can test it.
I`m lost and getting nuts, please assist...