Working with variables in Adobe Flash ActionScript 2.0

Scores and variable scoping

Flash has three different types of variable scoping. Variables can be scoped to functions, where they act as local varibles that only live for the life of the function. They can also be declared as global variables by declaring them at the _root level.

Timeline variables are accessible to all frames in that timeline, but not to any movie clips within that same timeline. When we want to access variables in other timelines, we need to traverse the movie clip hierarchy using relative or absolute pathing.

Local variables take precedence over global variables.

Variables in movie clips and buttons have different scopes. A variable created on a movie clip with the on or onClipEvent() handlers, will be local to the clip that the declaration is on. A variable declared inside a button with the on() handler.

Scoping is an important concept that you'll get used to dealing with in programming. Since properties behave like variables, they follow similar scoping rules as do variables.

We haven't yet discussed function scoping. Functions are a way of declaring a series of commands that we can reuse over an over in our program. We will cover this subject in detail in later sessions, just remember that variables declared inside functions are local to those functions.

We discussed in a previous lesson how you can create a dynamic text box to keep track of a variable. In order to do scoring for our game, you will need to keep track of a variable. You can call this variable score and declare it on the first keyframe of your movie (not inside a movie clip). Whenever an events that warrants a score change (you just hit one of the blocks), you can add to this variable.

blog comments powered by Disqus