Jump to content
xisto Community
Sign in to follow this  
novoAlias

Global Variables In Actionscript 3 Flash CS3

Recommended Posts

I have never quite understood why global variables are considered to be uncouth in OOP, but it is. However sometimes it is just convenient to have a variable that you can access from anywhere in your flash movie, and this is how you do it.First of all make a folder called testGlobals for your project, all your files will go in this folder.Now we are going to produce a package that holds our variablesThen, open up notepad and paste the following code in:

     package       {          public class globals          {              public static var data:Object = {};			  public static var varHello:String = "World!";          }      } 

Save this code in the folder as globals.asCreate a new flash file (as 3.0) create a layer called 'actions' and another one called 'content'In the first frame we want to import the package and produce some variables, so under actions put this code:

import globals;	globals.data.var1 = "I am variable one";	globals.data.var2 = "variable one sucks eggs";	globals.data.var3 = "variable two is my mother";	gotoAndPlay(2);

In the next frame under the content layer put four buttons with the instance names 'btn_var1', 'btn_var2', 'btn_var3' & 'btn_varhello' respectively, along with a textbox called 'txt_main.'Under actions put this code:

btn_var1.addEventListener(MouseEvent.MOUSE_OVER, f1);btn_var2.addEventListener(MouseEvent.MOUSE_OVER, f2);btn_var3.addEventListener(MouseEvent.MOUSE_OVER, f3);btn_hello.addEventListener(MouseEvent.MOUSE_OVER, f4);function f1(e:MouseEvent):void {txt_main.text = globals.data.var1;}function f2(e:MouseEvent):void {txt_main.text = globals.data.var2;}function f3(e:MouseEvent):void {txt_main.text = globals.data.var3;}function f4(e:MouseEvent):void {txt_main.text = globals.varHello;}stop();

Now when you press ctrl+enter your movie should run and the text should change to the variables from frame 1 when you move your mouse over each button.Remember these variables can come from anywhere and run as long as the movie does, the latest version I have loads data from an XML file and passes the variables between movieclips on the stage.Hope someone finds this as useful to know as I did :D

Share this post


Link to post
Share on other sites

Quite well explained. I know some flash myself, tough I don't really use variables unless I really need. You've also used functions in this tutorials so I think it'll be quite effective to explain those to. Anyway good job!

Share this post


Link to post
Share on other sites

Hi novoAliasThis is very helpfull indeed, thanks. The loading of the external xml-file puzzle me though, (actionscript is new to me). Could you send an example of how you do it? Of course, only if you have the time. It will be greatly appreciated. :)

Share this post


Link to post
Share on other sites
CorrectionGlobal Variables In Actionscript 3

You said:

"In the next frame under the content layer put four buttons with the instance names 'btn_var1', 'btn_var2', 'btn_var3' & 'btn_varhello' ..."

I think that last button should be called "btn_hello" not "btn_varhello"

I like the example scripts!

Share this post


Link to post
Share on other sites

thanks for the info! Puts me a little closer to what i need but i dont know how to implement it usefully in my project..What Id need is to implement it something like if (global.hello==true){MovieClip(parent).container.gotoAndPlay(5);}if(global.I am variable one==true){this.gotoAndPlay(11);}etc etcId need to make things happen according to which var is true?can that be done somehow?

Edited by jan33 (see edit history)

Share this post


Link to post
Share on other sites

oh.l. i get it! :))))



i made a movieclip on a higher level with 5 frames in it each containing a different shape object and a this.stop();, left the first frame empty, labeled it toDo, then added this to the second actions frame:

btn_var1.addEventListener( MouseEvent.CLICK, onClicked );function onClicked( e : MouseEvent ):void {	//txt_main.text=globals.data.var1;	toDo.gotoAndStop(2);}btn_var2.addEventListener( MouseEvent.CLICK, onClicked2 );function onClicked2( e : MouseEvent ):void {	//txt_main.text=globals.data.var1;	toDo.gotoAndStop(3);}btn_var3.addEventListener( MouseEvent.CLICK, onClicked3 );function onClicked3( e : MouseEvent ):void {	//txt_main.text=globals.data.var1;	toDo.gotoAndStop(4);}btn_hello.addEventListener( MouseEvent.CLICK, onClicked4 );function onClicked4( e : MouseEvent ):void {	//txt_main.text=globals.data.var1;	toDo.gotoAndStop(5);}this.stop(); 

THANKS! :P
Edited by jan33 (see edit history)

Share this post


Link to post
Share on other sites
btn_var5.addEventListener( MouseEvent.CLICK, onClicked );function onClicked( e : MouseEvent ):void {	//txt_main.text=globals.data.var1;	if (txt_main.text==globals.data.var3) {		toDo.gotoAndStop(3);	}	if (txt_main.text==globals.data.var2) {		toDo.gotoAndStop(2);	}	if (txt_main.text==globals.data.var4){		toDo.gotoAndStop(4);	}	if(txt_main.text==globals.varHello){		toDo.gotoAndStop(5);	}}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.