Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Tutorial 1 the variables

This is a discussion on Tutorial 1 the variables within the Actionscript for beginners - tutorials forums, part of the Tutorials category; Actionscritp 3.0 - Tutorial 1 The variables What are the variables? The easiest way to think about it is as ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials > Actionscript for beginners - tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  10 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 01-11-07, 06:47
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Tutorial 1 the variables

Actionscritp 3.0 - Tutorial 1
The variables


What are the variables?
The easiest way to think about it is as a box.
A box? Yes, just a simple box.
This box can be given a name and can contain things. For Actionscript, it will have a name and contains values or objects.
A box can contain different type of values so we will to start by telling our variable what type of values it can contain.
A variable can be local or global:
- local: if the variable is defined into a function or into a loop, so once the function code is carried out, the variable will be automatically deleted from the Flash?s memory. I will explain it to you later on in more details.
- global: this variable is always kept in memory and so is always accessible

This is the way to declare a variable:
Code:
var miles:Number;
var declare the variable to Flash
miles is the name of the variable
:Number; is the type of value that the variable can contain. A numerical value in this case.

Following the idea of the box. I tell Flash to create a box with a label name (miles) and also specify to Flash that the box can only contain values of numerical type such as 0, 3, 124, 0.4, 654.332?

Now, I will assign a value to my variable the following way:

Code:
miles=10;
PS: I also could have assign the value at the same time as the declaration the following way:

Code:
var miles:Number=10;
Little parenthesis: :Number is define as the Data Type of the variable.
With Actionscript 1.0, we were used to simply write var miles=10; and in the OOP there is nothing worst then doing so.
As an advice, I recommend you to always specify the Data Type. If you wanted next to start writing code on an AS file, without the Data Type, Flash would return an error. So better get used to it from now on.

Next, I write:

Code:
trace(miles);
I tell Flash to show me what is in the box, so flash will open the box and will show me the value contained into it.
I obtain the following output:
10

I said earlier on that the variable Miles can contain only numerical values as it has been data typed as Number (:Number).
Let us see if it is correct and try to insert a value of String type into the variable miles:

Code:
miles='Davis';
if I publish the SWF, Flash would return the following error:
1067: Implicit coercion of a value of type String to an unrelated type Number.
I tells me that the type of value is wrong and that I am trying to assign a String value to a variable data typed Number:

Instead, if after writing var miles:Number=10; I would add:

Code:
miles=15;
with a trace(miles), I would obtain the following output:
10
15


We can understand now that we can change the value of the variable as wanted keeping in mind to always use the same data type declared

With the following example, I add a value to the existing value of miles:

Code:
miles+=10;
this syntax tells Flash:
open the box named miles, take its value, add 10 and close again the box.
Therefore, it adds a value of 10 to the already existing value.
In fact with trace (miles); I get the following output:
10
15
25

It is the same as saying miles= miles+10;

The spontaneous question is which types of values can I assign to a variable?
Answer: every type of value of the built-in classes that exist in Flash, therefore Number, String, MovieClip, TextField, etc etc
A complete list of the existing classes in Flash can be found to this link:

Migration

To help you better to understand, here are some few examples.

I create a variable of type String and I assign to it a string value:

Code:
var my_name:String="filippo";
and with trace(my_name) I obtain the following output:
filippo

it is important to write the string (as it is a string) in between the double quote ?filippo? otherwise if we would write:

Code:
var my_name:String=filippo;
Flash would return the following error:
1120: Access of undefined property filippo.
rightly telling us that it cannot find any variable named filippo.
In fact, if we write =filippo, Flash is looking for a variable with such name to assign its value to the variable ?my_name?.

Another example, I create a variable of type MovieClip:

Code:
var clip:MovieClip=new MovieClip();
in this case, we have a new syntax not seen before: the operator new.
This operator allows Flash to understand that it has to create a new MovieClip as the variable clip will contain a MovieClip.
So, using var clip:MovieClip=new MovieClip(); we are telling Flash to create a new empty MovieClip.
Going further on, we should then create an instance of the MovieClip class but it is not yet the right time to get into this and at this point would only confuse the basic idea.

Another example, I create a variable a type textField:

Code:
var field:TextField=new TextFiled();
once again, we find the operator new.

Stay tuned to learn more about it!

Source files:
Attached Files
File Type: zip tut_beginners_1.zip (5.7 KB, 123 views)

__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !

Last edited by Flep; 05-06-08 at 19:02..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Passing variables from HTML to Flash CS3 Flep Tutorials 24 25-11-08 22:36
Actionscript 3 variables and functions from child movieclip coma Actionscript 3.0 newbies 9 25-11-08 21:41
Actionscript 2 declaring variables Aaron24 Flash CS3 eng 9 24-08-08 21:43
Actionscript 3 Declaring variables in AS 3.0 Pherankh Actionscript 3.0 newbies 0 24-06-08 22:13
Displaying PHP Variables in Flash chosendesigns PHP | mySQL | Flash CS3 1 14-05-08 13:49


All times are GMT. The time now is 22:24.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406