Home » ActionScript 2.0, ActionScript 3.0

ActionScript Basics part 3

24 November 2010 No Comment
Share on Facebook

In Previous two articles we learned about Statements and comments in ActionScript.

Previous Entries:
ActionScript basics part1

ActionScript basics part2

ActionScript basics part3

ActionScript basics part4

ActionScript basics part5

ActionScript basics part6

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

var someMsg;

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.

Define datatype

Datatype defines what kind of data a variable going to hold,

for example
A variable can hold number, text or String, Boolean – true|false

Syntax for variable

var someMsg:String;

Here the datatype we declared is String which can hold serious of text.

Some of the Datatype used in ActionScript are

  • String
  • Number
  • Int
  • Boolean
  • Uint

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

Note:
While assigning some value from variable to another variable the datatype of both variable should of same datatype.

GET THE UPDATES VIA EMAIL

We don't share your email anywhere, grab our rss via feedburner


Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.