Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,602 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 950 people online right now. Registration is fast and FREE... Join Now!




AS 2.0 Basics 1 - Variables

 
Reply to this topicStart new topic

> AS 2.0 Basics 1 - Variables, [Flash 8]

BetaWar
Group Icon



post 15 Jul, 2008 - 11:20 AM
Post #1


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:

CODE
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:

CODE

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:

CODE
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:

CODE
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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/23/08 02:08AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month