Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Global Variables In Actionscript 3


6 replies to this topic

#1 novoAlias

    Newbie [Level 2]

  • Kontributors
  • PipPip
  • 37 posts
  • Gender:Male
  • Location:Auckland
  • Interests:Places<br />In the sun, on the snow, under the water. And after all that, on a good mattress!<br />Music Tastes<br />House, D&amp;B, Trance, Electronica, Lounge, Classical, Jazz, Acoustic, Rock, Brit Indie<br />People<br />If you have to ask...<br />Philosophy #1<br />Work hard, Party harder.
  • myCENT:89.51

Posted 12 February 2009 - 03:05 AM

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 variables
Then, 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.as

Create 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

#2 andreip

    Advanced Member

  • Kontributors
  • PipPipPipPipPipPipPip
  • 148 posts
  • Gender:Male
  • Location:Romania, Bistrita
  • Interests:Soccer, Basketball, Web Design, Photoshop, Flash, Music, Movies, Friends.
  • myCENT:65.83

Posted 03 March 2009 - 05:23 PM

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!

#3 kadme

    Newbie

  • Kontributors
  • Pip
  • 1 posts

Posted 21 July 2009 - 12:52 PM

Hi novoAlias
This 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.

:)

#4 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 26 February 2010 - 04:51 PM

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!



#5 jan33

    Newbie

  • Kontributors
  • Pip
  • 3 posts

Posted 23 May 2010 - 09:57 AM

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 etc

Id need to make things happen according to which var is true?
can that be done somehow?

Edited by jan33, 23 May 2010 - 10:02 AM.


#6 jan33

    Newbie

  • Kontributors
  • Pip
  • 3 posts

Posted 23 May 2010 - 10:11 AM

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, 23 May 2010 - 10:13 AM.


#7 jan33

    Newbie

  • Kontributors
  • Pip
  • 3 posts

Posted 23 May 2010 - 10:38 AM

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);
	}
}





Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users