Skip to main content
July 12, 2006
Answered

Learning ActionScript - Var declarations

I'm trying to learn ActionScript, and while the fog is starting to lift there are little things like this that confuse me.

I have an existing audio player project that I'm revising according to the Best Practices docs.

In the first frame of the Action layer it has a script that includes variable declarations like this:

this.autoStart = 0;

It appears this code declares a variable called autoStart and sets its initial value to zero. In the variable list window at runtime it comes up as:

Variable _level0.autoStart = 0

So It's working as it is, but in reading the documentation it appears it would be better to write the code like this:

var this.autoStart:Number = 0;

However that doesn't work. It generates an error when I try to test the movie.

If I change it to:

var autoStart:Number = 0;

It works.

All I've done is remove the "this." from the declaration.

I'm trying to understand why the notation this.autostart works without the var keyword, but not with the var keyword.

Thanks!

Tony


This topic is closed to new replies. Start a new post to keep the conversation going.
Newsgroup_UserCorrect answer
Inspiring
July 12, 2006
Tony,

> in reading the documentation it appears it would
> be better to write the code like this:
>
> var this.autoStart:Number = 0;
>
> However that doesn't work. It generates an error
> when I try to test the movie.

The var statement only works with non-pathed references to a variable.
You can write this ...

var autoStart:Number = 0;

... and the variable will be declared and scoped to whatever timeline holds
this script. If you're in the main timeline, the above is essentially the
same as writing ...

this.autoStart = 0;

... as long as "this" refers to that same timeline. Really, var handles
scope, which determines "where" an object resides. If you happen to have a
movie clip in the main timeline with the instance name mc, and if you want
mc to contain this autoStart variable, you could either do this ...

// A)
// in a script in the main timeline
mc.autostart = 0;
// or
this.mc.autoStart = 0;

// or B)
// in the timeline of mc
var autoStart:Number = 0;

All three of those are basically equivalent, because all three of those
scopes the autoStart variable to mc. Note that the only time you can use
strong typing (:Number) is with the var statement.


David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."


8 replies

Inspiring
July 12, 2006
Just something that I find to be in my "best practices" maybe not official, but you mention _root.varName and _root is one of my pet peeves.

If the swf you are authoring ever needs to be loaded into another the _root will change. _root is an absolute reference, so just like if you put a folder inside another folder its absolute path will changed. So after being burned several times (hey you only have teach me 3 or 4 times!) I personally stopped using _root at all! Instead I almost always use relative paths – sometimes if I'm having a difficult time with scope I will use _root as a temporary patch to be sure the problem is with the scoping and not something else.

So if the empty clips are on the main timeline and they need to reference their parent timeline I would use _parent.myVarName.
Translate
July 13, 2006
Rothrock,

I agree with you. I'll be changing all those instances of _root that I can figure out.

Wouldn't those change to this._parent.myVarName?

Or is the this. optional?

Also, because I'm relocating the code from the movieclips into the main timeline I won't even need the path for many of the variables which are timeline variables anyway. I think.

We'll see.

Thanks!

Tony
Translate
Inspiring
July 12, 2006
tonyoppenheim watch out for that David Stiller – he may seem calm, but he is really a seething cauldron! :) (PS that is just a long-running joke!)

The only thing I wanted to add about var this.someName generating an error. var says make this variable local to this timeline so having any kind of dot syntax after a var doesn't make sense.
Translate
July 12, 2006
quote:

Originally posted by: Rothrock
tonyoppenheim watch out for that David Stiller – he may seem calm, but he is really a seething cauldron! :)



Yes, I'm seeing that energy... :)

I went back and re-read some of the docs on var again, and I see where I went wrong there.

I've been reading the best practices docs on ActionScript and they talk a lot about always using this. and always explicitly typing variables.

This is just a really good example of how a little (very little) knowledge can be dangerous! :)

Thanks!

Tony

Translate
Newsgroup_UserCorrect answer
Inspiring
July 12, 2006
Tony,

> in reading the documentation it appears it would
> be better to write the code like this:
>
> var this.autoStart:Number = 0;
>
> However that doesn't work. It generates an error
> when I try to test the movie.

The var statement only works with non-pathed references to a variable.
You can write this ...

var autoStart:Number = 0;

... and the variable will be declared and scoped to whatever timeline holds
this script. If you're in the main timeline, the above is essentially the
same as writing ...

this.autoStart = 0;

... as long as "this" refers to that same timeline. Really, var handles
scope, which determines "where" an object resides. If you happen to have a
movie clip in the main timeline with the instance name mc, and if you want
mc to contain this autoStart variable, you could either do this ...

// A)
// in a script in the main timeline
mc.autostart = 0;
// or
this.mc.autoStart = 0;

// or B)
// in the timeline of mc
var autoStart:Number = 0;

All three of those are basically equivalent, because all three of those
scopes the autoStart variable to mc. Note that the only time you can use
strong typing (:Number) is with the var statement.


David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."


Translate
July 12, 2006
David,

The situation in this audio player is that there is a variable autoStart that needs to be read by scripts that are part of movieclips that are on other layers of the main timeline.

It seems like the declaration:

var autoStart:Number = 0;

Creates a "timeline" variable.

If I understand things correctly a timeline variable's scope allows it to be accessed by any script on that same timeline.

This existing app has a symbol called emptyMC, three instances of which were placed outside of the stage. Each of them is on their own layer which is part of the main timeline. On the left side of the Action panel there are symbol definitions. The symbol emptyMC has two short scripts. One on the first frame and one on frame number 5.

I understand how the scripts inside the symbol work. I don't understand why the developer used 3 instances of the same symbol (but maybe that's another question). Each of these symbols has the same timeline, so again, if I understand what I'm looking at it seems like those same scripts could be executed in 3 places.

Maybe it doesn't matter?

The way the app is designed only one of the 3 instances of emptyMC is ever called with gotoandplay(), and the script on the first frame of each clip just says stop();, so it appears the script on frame 5 only ever gets executed from one of the instances of emptyMC when it is called with:

_root.timingMc.gotoAndPlay(2);

However, each of the 3 instances of emptyMC does have a different script listed under Scene1 in the action panel, and all of those scripts _are_ executed.

If I click on them in the left side of the action panel (where they are listed under Scene1) they each have different scripts. I'm not sure I understand where those scripts exist (are they timeline scripts that are not on a frame?, or what?). If I click on the first frame next to a layer containing one of the emptyMC clips no script is displayed The main timeline only has one frame.

It seems like they must be timeline scripts because the they are able to access the timeline variable autoStart that we were originally discussing, but that's a little confusing since the scripts don't appear to be connected to a frame on any timeline.

BTW, I've seen many of your answers on the forum already, and I found my way to your site over the weekend. Your blog posts are very helpful!

Thanks!

Tony
Translate