ActionScript Basics part 3
In Previous two articles we learned about Statements and comments in ActionScript.
ActionScript basics part1
In this article we are going to learn about the followings
- What is a variable?
- Declare a variable
- Define datatype
- Assigning value to a variable
- Doing calculation and output a variable
What is a variable?
Remember any mathematical calculation we studied in our school,
Area = side * side (Formulae to find area of square)
Here side going to have some value, which we multiple and assign the final value in Area.
So each of the is a variable going to hold some number.
In programatic way of variable may have not only numbers, it may also have Text, Character, Boolean (True|False).
Declare a variable
Open a Flash document
Select the Empty Frame and Press “F9″ Function Key to open the ActionScript panel
Type the following
At this point, the varible is declared. This means that the container exists, but currently has no associated value.
This is the bare minimum you need to declare a variable. The word “var” is known as a keyword, meaning it’s reserved by the ActionScript language and has a specific
meaning. In this case, it declares a variable. It needs to be followed by the variable name you want to use; if you don’t follow the var keyword with a variable name
you’ll get an error when you try to test the movie.
Datatype defines what kind of data a variable going to hold,
A variable can hold number, text or String, Boolean – true|false
Syntax for variable
Here the datatype we declared is String which can hold serious of text.
Some of the Datatype used in ActionScript are
Assigning value to a variable
Once we declare a variable and define the datatype we can assign a value to the variable.
var someMsg:String = "Hello World!";
here, we have assigned serious of characters “Hello World!” to the variable someMsg using the equal symbol which is called assignment operator.
Doing calculation and output a variable
We are going to create some variables of datatype Number and going to do some calculations.
var squareArea:Number; var side:Number = 10; squareArea = side * side; trace("Area of square: "+squareArea);
here, we have created two variable of name squareArea, side.
And we have use multiplication operator to do multiplication and assigned the final output to the squareArea variable
While assigning some value from variable to another variable the datatype of both variable should of same datatype.