Be a Supporter!

Global Variables? [AS3]

  • 1,349 Views
  • 3 Replies
New Topic Respond to this Topic
blakemoso
blakemoso
  • Member since: Apr. 2, 2011
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Global Variables? [AS3] 2012-01-31 20:21:28 Reply

I've been having a bit of troubles figuring out this little problem...

I've got a variable called 'score' on the main timeline, and then I have some actions on a frame of a movieclip's timeline...

on that frame, I want to have a line saying 'score++;'... but, of course, I can't just access the variable from there like that... I tried googling this to find some answers, but nothing I can find seems to work for this.

I feel like this is a stupid thing to be having problems with, so sorry if I sound like an idiot.
Any and all help is greatly appreciated!


BBS Signature
Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to Global Variables? [AS3] 2012-01-31 20:41:38 Reply

There is no _global variables like there was in AS1-AS2. Now you have to acess these variables through a static class. It is pretty simple, create an Actionscript file and name it GlobalVariables for example and create a static function or static variables for your score, then just call it through the class.

Example:
GlobalVariables.score++;
GlobalVariables.addScore();

etc

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Global Variables? [AS3] 2012-02-01 02:29:26 Reply

Do not create a global class.

Instead, dispatch an Event, listen for it where "score" is defined and modify it accordingly.

Psionic3D
Psionic3D
  • Member since: Sep. 19, 2007
  • Offline.
Forum Stats
Member
Level 10
Game Developer
Response to Global Variables? [AS3] 2012-02-09 07:06:12 Reply

I use MovieClip(root). to get access to variables declared on the main timeline e.g.

MovieClip(root).score ++;

MovieClip(root).highscore = MovieClip(root).score;

This is apparently very bad practice and a little long-winded but I have made several games now and used this throughout without problems, Although I'm using AS3 I still like to do some things AS2 style ;-)