0
votes

OK, I'm not very used to Flash and I've been stuck for hours on this problem, so any help would be really appreciated...
I have this quite complicated FLA which is basically set up like this :

  1. "Scene 1"

    • some loading animation (and code)
    • AS code receiving values from flashvars (in the HTML code) and validating them
    • at the end of the Scene 1 timeline, we enter a second movie clip:
  2. "map-total"

    • here a map is displayed and various elements appear on it
    • nothing interesting here actionscript-wise
    • but among those elements, a movie clip :
  3. "btn-vb"

    • here, various layer; one of those contains yet another movie clip (vb_anim)
    • also contains some AS code to make vb_anim grow bigger as the mouse rolls over it, and reverse when the mouse leave (and react to click)
  4. "vb_anim"

    • here we find several layers with some basic animation.
    • among those layers, three represent different variants of one same button (available, not ready, forbidden).
    • each of those three layers contain one instantiated object (with some tweening)

What I need is this:
Depending on the flashvars I receive in Scene 1, I need to display or hide one or several of those three objects in "vb_anim".

In order to access the variables set in Scene 1, I use the following shortcut:

var global:MovieClip = MovieClip(root);
// allows for access to the "root" variables
// e.g. global.myVar

I already use the same code in "btn-vb" (I need to access the information provided by the flashvars in order to define the onclick event) and it works like a charm.

However, it appears that when called from inside "vb_anim", MovieClip(root) evaluates to null. So it's impossible to access the variables from Scene 1, and Flash throws a "TypeError: Error #1009" when I try to do so.

Since "vb_anim" is simply embedded in "btn-vb", I don't understand why it can't see MovieClip(root) while its parent can.

Any idea of what I did wrong?

2
can you post more code, or your .fla? - BadFeelingAboutThis

2 Answers

1
votes

Variables you declare on the timeline of a MovieClip in the IDE belong to that clip alone, the code generator in the IDE will generate class fields from them for the classes it generates for the MovieClips you used. This is why it didn't work, I presume. I.e. you declared this variable in the place it is only accessible to the code associated with the MovieClip that contains that code, but contrary to what you called it, it is not global.

ActionScript doesn't really have the concept of global variables, variables can be either scoped to a class (fields, or also known as members) or be package-level variables (a seldom used and largely discouraged practice), while the second kind may some times serve the purpose the global variables do, it provides no mechanism for ensuring uniqueness and thus being a potential danger of mistakes and / or a security risk. There isn't a way to fix this in AS3, that's why it is better to avoid it altogether.

But this is not the only problem with your code. You are casting root to MovieClip simply because it will let you avoid type checking (making your code more more prone to errors). The proper way to resolve the situation is to create a class for the root container and assign it through the means of IDE (IDE calls this class "the document class") to the root container. Then, whenever you use root, you will be able to cast it to that class, allowing the code assist and the compiler to help you write the safer code faster.

0
votes

All MovieClips anywhere on the timeline (or added through ActionScript, except actual Loaders) have a loaderInfo property that points to the same loaderInfo you already are reading the parameters off of, so you don't need to refer to the root.

Besides, it's REALLY bad practice for a child to know anything about its parent/grandparent/etc. See encapsulation. Instead, have the child expose a property (variable) that the parent can populate with the correct information. So, your main timeline gives the information to btn-vb, which both uses it and passes it on to vb_anim.