Page 1 of 1

AS 2.0 Basics 1 - Variables [Flash 8] Rate Topic: -----

#1 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,149
  • Joined: 07-September 06

Post icon  Posted 15 July 2008 - 12:20 PM

Variables in Flash (AS 2.0) are similar to those in many other languages with a few quirks and differences that make them unique. In this tutorial we will be covering how to declare variables, what types of variables there are and why you want to declare variable types.

Accessing the Actionscript Panel
An important note before we start. To access the actionscript panel in Flash you can either press F9, or click on the Actions - Frame panel on the bottom of the default Flash workspace.

Super easy variables in Flash
Sometimes you aren't wanting to have to declare a variable or its type, and are instead just wanting to add in a new variable with a set value. This can be done in Flash, like with Javascript and other languages by simply using code as such:

myVar = 10;


Which sets the variable called myVar to 10. But this isn't all that Flash can do with variables.

Declaring Variables the proper way
Declaring variables in Flash is comparable to declaring a variable in Javascript, or PHP, or other like languages. You simply need to place var and then the name of the variable.

For instance you could do something like so:

var myVar = 10;



The myVar portion of the code is setting th evariable name and then = 10 is setting myVar to 10.

Variable Types and their quirks
One thing that Flash takes from lower level programming languages is the ability to specify the variable type, such as in C/C++ where you declare a whole number by using int myVar = 10;. However Flash takes a different approach and allows you to decide if you want to specify the variable type, or just have it a generic variable.

If you were wanting to create the same variable as above and only allow it to be a number (this includes floats and ints, basically any and all numbers) you would change the code to specify the variable type:

var myVar:Number = 10;


That creates the variable myVar as a number and sets it equal to 10. Now, ifyou were to take the same code as above and change it to a string it would return an error. var myVar:Number = "Text"; would return Type mismatch in assignment statement: found String where Number is required.

So, now I know that many of you are wondering why you would ever want to declare a variabel type. What does it do for you?
Luckily, Flash has also added in some nice parser functions if you declare the variable type. Once you have done the declaring and use the variable again the parser will give you auto-completion options to make the coding go faster, and let you know all the functions that are associated with that variable type.

For instance, if you are using a string variable var myStr:String = new String("Text"); and then wanted to trace (output) the value that myStr has you could do something like so trace(myStr.toLowerCase()); which would trace the value as all lower case characters. The other possible functions that are available for strings when you use the dot property of the string myStr. are as follows:

charAt charCodeAt concat fromCharCode indexOf lastIndexOf length slice split substr substring toLowerCase toUpperCase


Each of which give you tips on how to use them.

Before we end
Now you should have a fairly decent understanding of Variables in Flash, if you want to find all of the available variable Types in your version of Flash (works at least in Flash 8), in your Actions Frame click the blue plus sign (+), the tooltip is "Add a new item to the script", in the top left of the textbox and scroll down to Types.

The End
I hope you were able to learn a lot from this tutorial and understand why it is useful to declare your variables and their types.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1