Actionscript 3.0 for beginners: lesson # 5

Who has started to develop code with scripting languages ​​such as JavaScript or ActionScript the same, may not know all the concept of data type. With some high-level languages, in fact, it used to - at best - to declare variables without giving a specific data type, not counting those languages ​​that do not require any statement (such as PHP - not in the strict version 5 ). Coming from C, by contrast, would not even know how to use a variable without declaring it in a rigorous way.
In the example below we see the declaration of two variables without specifying the type as it will contain:

1
2
; mio_numero var = 0;
; mia_stringa var = "text string";

The same Actionscript, as announced, provides for the non-declaration of types and variables! This latter behavior is enforced by removing the tick in the box "Strict Mode" Actionscript 3.0 Settings dialog:

Flash CS3

So doing a code like this will not produce any error below:

1
2
3
; mio_numero var = 3;
mio_nove = 9;
mio_nove + mio_numero + 5 ) ; trace (mio_nove mio_numero + + 5);

. At line 2 the variable mio_nove not only contains the type, but it's not even been declared using the keyword var . Although it retained the ability to declare variables in this mode "bland", is far more correct, useful and effective to specify the data type associated with our variable:

1
2
uint = 0 ; var mio_numero: uint = 0;
string = "stringa di testo" ; mia_stringa var: string = "text string";

This is the correct way to declare variables, useful for improving the performance and robustness of our application.

Advantages in the use of types

There was a time in which a programmer had to save memory in bits! When the code had a few kilobytes available, use 8-bit to keep the months of the year was really a huge waste. ). With 4 bits, in fact, we can store 16 values, from 0 ( 0000 ) 15 ( 1111 ). Today, of course, times have changed. A personal computer has on average a few Gig of RAM (mine has 8!). It is therefore not commonly used to pay more attention to the "consumption" of RAM and resources.
For a closer look, then, the problem of increased consumption of RAM is not just a "waste" end in itself, a kind of poor management can make the code slower since it requires the compiler, interpreter and working on the final executable types more "capacious" than necessary.

Actionscript 3.0 has introduced some new data types that allow you to develop higher performance through certainly the indication - prevention - the type of data to be treated. A classic example concerns the cycles for . Starting from this table:

  • Number : (64 bits) from -9,007,199,254,740,992 to 9,007,199,254,740,992 (for integers)
  • int : (32 bit) -2,147,483,648 (-2 31) to 2,147,483,647 (2 31 - 1)
  • uint : (32 bits) from 0 to 4,294,967,295 (2 32 - 1)

Write:

1
2
3
var i : Number = 0 ; i < 100 ; i ++ ) { for (var i: Number = 0, i <100; i + +) {
/ / ...
}

is clearly a "waste of resources", much better and would be sufficient:

1
2
3
var i : uint = 0 ; i < 100 ; i ++ ) { for (var i: uint = 0; i <100; i + +) {
/ / ...
}

In general it would be a good idea to refer to this chart:

  • Number : any numeric value, with or without decimals
  • int : an integer with no decimal places
  • uint : unsigned integer, which is a non-negative integer

This means that if we store integer values, ie no decimals, positive, uint data type is perfect! If we also need the "sign", ie negative integers, we can make use of int . In cases of very large numbers and decimal, then the type Number is the only solution available to us.

4 comments to "Actionscript 3.0 for beginners: lesson # 5"

  1. July 25, 2009 Alexander

    Hello I'm going crazy.
    I wanted to know once and for all whether there is AS3 way to get into a scene using a button?
    That is:
    I have 2 Scenes
    The first is called HOME.
    The second GALLERY
    I want to go in the HOME GALLERY.

    Please ... HELPAMI!!

  2. August 3, 2009 Giovambattista Fazioli :

    @ Alexander: Of course you can do it! Just use the classic command gotoAndPlay() : The first parameter is the name of the scene, usually omitted when using a single scene. See here for an example

  3. August 3, 2009 Alexander

    @ Giovambattista Fazioli: Hello thank you for letting me respond.
    just that I'm looking for the scene changes from ActionScript 3.0, whereas that very
    You've indicated you kindly for ActionScript 2.0

    Thank you again ..

  4. August 3, 2009 Giovambattista Fazioli :

    @ Alexander: but it is the same for AS 3

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL