Actionscript Course

To work in ActionScript 3 with an element added in the Flash Stage we use its instance name (the name written in the Properties panel, in the box whith "<Instance Name>").
In AS3 code you can also use objects stored in the Library panel (images, symbols), without adding their instance on the Stage. This is more advantageous because multiple instances of the same object from the Library can be added dinamically and interatively in the Flash presentation, using ActionScript.

To be able to use an element from Library in AS3, it must be stored in a special class, then, this class can be accessed in your code to create and use new instances.
To store the objects from Library in a class, you must enable the option "Export for ActionScript", which is found in the "Advanced" section, in the window which is opened by right-clicking on the object in the Library panel, and choose the Properties option (see in the image below).


Try the following example.
  1. Open a new Flash document, draw a square on the Stage and convert it into a MovieClip Symbol (from Modify -> Convert to Symbol). You can give it a name, for example Square (it is usefuf to give a meaningful name for the Symbol, making it easier recognized in the Library panel).
  2. The Symbol is automatically added in the Library panel. Delete the square on the stage and open the Library panel (from Window -> Library).
  3. Right-click on the Symbol's name (Square) and choose Properties. Flash will open a window like in the image below. Click Advanced, to can access the options in the advanced zone.
    Properties objects in Library
  4. Check the "Export for ActionScript" button, it will store the Symbol in a class (whose name is added in the field "Class"), and gives the posibility to use it in the ActionScript script.
    Flash automatically adds the name of the Symbol in the Library as the class name. It can be modified, but usually it is better to have the same name for the class and object. This name appears also in the column "Linkage" in Library (as you can see in the image above).
  5. Once the "Export for ActionScript" option is enabled and the class name is set (Square), press OK. The class that represents this object is created and can be used in ActionScript code.
    - "Square" becomes a child class of a base class, specified in the field Base class (in this case "flash.display.MovieClip").
    - Now you can create and use in your ActionScript code Instances of the Square class, with the syntax:
                    var instance_name:ClassName = new ClassName();
  6. Right-click on Frame 1 in Timeline, choose Actions to open the "Actions panel", and add the following code:
    // Defines a function that creates an instance of the Square class
    // with parameters for 'x' and 'y' distance, and rotation
    function addSquare(dist_x:Number, dist_y:Number, rot_z:Number):void
    {
      // Creates instance of Square class
      var ins_square:Square = new Square();
    
      // Sets the distances 'x', 'y', and rotation
      ins_square.x = dist_x;
      ins_square.y = dist_y;
      ins_square.rotationZ = rot_z;
    
      addChild(ins_square);         // Add the instance in the presentation
    }
    
    // call this function 3 times
    addSquare(38, 38, 0);
    addSquare(130, 130, -19);
    addSquare(250, 220, -45);
    
    - This code creates and adds in the Flash presentation three instances of the object Square, each with different values for the distances 'x', 'y' and for rotation.
    - Press "Ctrl+Enter" to see the result.
The Flash player will display three squares like in the image below.
Example AS3 with objects in Library
- This technique can be used with any object added in Library (Symbols, images, sounds).

The Class associated to an element from Library can be edited. You can add instructions and methods that will be applied to the instances created with that class.
Right-click on the object in the Library panel, and choose Edit Class.
Add the code you want, then save the file with this class in the same folder as the FLA document (the name of the file must be the same as the name of the class).


To download the FLA file with the example from this lesson, click: ActionScript with objects from Library.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Working with objects from Library in AS3

Last accessed pages

  1. Shape Tween - Flash Animation (6149)
  2. The Mastery of Love (7440)
  3. Get Mime Type of file or string content in PHP (6230)
  4. Countdown Timer with starting time added into a form (11533)
  5. Disable button and Enable it after specified time (17533)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)