1

Ok, I want to be able to access certain variables from anywhere within a Flash file or Flash files loaded by that Flash file. How do I do it? I don't know what classes are, I don't want to learn what classes are, I don't want to import anything, I just want to be able to initialize and access certain variables from anywhere.

Thanks :)

Zevan
  • 10,097
  • 3
  • 31
  • 48
A-OK
  • 3,184
  • 10
  • 34
  • 42
  • as the old adage goes: "knowledge is power". – Chunky Chunk Dec 11 '10 at 03:53
  • They also say "ignorance is bliss". The truth is probably somewhere in the middle. Also since I'm asking a question on Stackoverflow instead of reading a reference book, it probably means I need a quick solution now rather than comprehensive understanding of the subject matter in two weeks. – A-OK Dec 11 '10 at 03:59

1 Answers1

8

Well, there is no more _global like there was in as2 - and since you don't want to use classes you can't use static variables (I can explain these if you're interested). So you're left with using variables on the root. For example, you can define a variable on the main timeline like this:

var myGlobal:Number = 100;

If you want to access it elsewhere ... that is, on the timeline of other movieClips you need to say:

MovieClip(root).myGlobal;

Which if you've never seen before probably looks absurd. Basically we are casting the root to a movieClip to give us access to its dynamic properties. Luckily, you can set it up so you don't have to keep writing MovieClip(root) all the time:

// do this on any movieClip where you want to access globals
var global:MovieClip = MovieClip(root);

trace(global.myGlobal);

So in the end its just one extra line of code to get the functionality back to the way it was in AS2.

Edit

  1. go to main timeline and add this to your actions: var myGlobal:Number = 100;
  2. make a new movieClip make sure its on the stage
  3. go into the new movieClip and add this to your actions :

    var global:MovieClip = MovieClip(root); trace(global.myGlobal);

  4. test your movie

Zevan
  • 10,097
  • 3
  • 31
  • 48
  • 2
    sorry, I don't understand that at all. I'm going back to AS2 where I can do everything – A-OK Dec 11 '10 at 01:59
  • heh. If you tell me what you don't understand I can try and explain. I'll add an edit that will maybe clear things up. – Zevan Dec 11 '10 at 02:01
  • Well I don't understand what is 'do this on any movieClip' and what if I have to do whatever-it-is on a hundred movieclips? Basically every button and every item on the page is a movieClip and most of them need to access certain global veriables (like the language used, user preferences etc) so I just can't be bothered to mess with it when in AS2 it just works. – A-OK Dec 11 '10 at 02:02
  • check out my edit, maybe that will help - I also uploaded an fla for you on filedropper (click the download this file link) http://www.filedropper.com/globals – Zevan Dec 11 '10 at 02:05
  • in as2 are you just using _global ? – Zevan Dec 11 '10 at 02:06
  • Will I still have to add something to every movie clip I have (50 so far and I'm just getting started)? – A-OK Dec 11 '10 at 02:06
  • 1
    ok, pretty much the same thing. You could use that exact same technique by using MovieClip(root).language anywhere you want you don't need to initialize the language var in a specific place... its a bit more typing but it will do the same thing as _level0 for the most part. I suggested storing MovieClip(root) in a variable so its easier to type, but you don't have to. It takes about a month to get used to as3 and its annoying month. But once you get it, its worth it. – Zevan Dec 11 '10 at 02:11
  • if your working on project with a rush timeline you might want to stick to as2 and start learning as3 when you have some time to deal with all the differences. – Zevan Dec 11 '10 at 02:12
  • Thanks, that's what I needed. It's a pain in the ass to type but I'll just add it to the client's bill. I got a book for AS3 but it took like 200 pages to get to 'Hello World' so I gave up. Nowadays I pretty much stick to jQuery and telling clients Flash crashes computers. Thanks for your help and for putting up with my learning boycott. :) – A-OK Dec 11 '10 at 02:17
  • you could always try my book on as3 ;) http://www.amazon.com/Learning-ActionScript-3-0-Rich-Shupe/dp/144939017X/ref=sr_1_1?ie=UTF8&s=books&qid=1292035040&sr=8-1 I also use a lot of jquery these days. – Zevan Dec 11 '10 at 02:37
  • 1
    Well got your book. Hello World is in chapter one so you're 200 pages ahead of that tosser Colin Moock. :) – A-OK Dec 11 '10 at 03:02
  • ha - thats pretty funny - hope you dig the book – Zevan Dec 11 '10 at 03:08
  • I like (this.root as Main).globalProperty instead. It makes more sense to me and it allows me to see the property in IDEs. – sfxworks Jul 11 '17 at 12:12
  • "Nowadays I pretty much stick to jQuery and telling clients Flash crashes computers." You should learn a language before you knock it. You hurt communities like that. – sfxworks Jul 11 '17 at 12:16