CIS 421 Flash> ActionScript 3 basics

ActionScript 3 basics

What's new about AS3?

Language fundamentals

  • Execution order, usually from top-to-bottom, left-to-right
  • Semicolon ends each instruction -- not required but recommended.
  • Debug with trace command which displays information in output panel during execution.
  • Variables: numbers, integer, unsigned integers, string, Boolean, Array, Object, + others
  • Conditionals:
    • If, else
    • switch
    • for loop, while loops -- loops tend to be very processor intensive -- frame & timer loops work better
  • Arrays
  • Functions for reusable code
  • Custom objects
  • this -- shorthand for the object or scope with which you are working
    • scope is the space within which the object resides
    • example: a movieclip embedded in another movieclip
  • if two words are strung together, the first word begins in lower case, and any other new words begin with Caps
  • Constants are written in all caps and use underscores: MOUSE_UP
  • Absolute or relative addressing of objects with dot notation
    • Absolute: root.mc1.mc2 would reference mc2 embedded in mc1, which is on the main timeline (root)
    • Relative: this.parent.mc1.mc2 would go up one level to its parent object, over to mc1, and into mc2, which is nested in mc1

top

Objects

Objects inherit attributes from their parents

Properties

Objects have properties.

In the SWF movie below, the movieclip instance "box" can have these properties:

  • location in pixel units: box.x =100; box.y=100;
  • scale in %: box.scaleX=.25; boxScaleY=.5;
  • scale in pixels: box.width=45; box.height=45;
  • rotation in degrees (0-360): box.rotation=90;
  • transparency in % from 0 to 1: box.alpha=.5;
  • visibility in Boolean: box.visible=false;

Events

  • Many kinds of events triggered by keyboard, mouse actions, etc.
  • In AS3, event handling is done by event listeners (very different from AS2 and AS1)
  • the addEventListener() method identifies the even and assigns the function to be executed to the object that is doing the listening.
  • Example for rotating box in SWF movie below:
    rotate_left_btn.addEventListener
    (MouseEvent.MOUSE_UP, onRotateLeft);

    rotate_left_btn is the object that is doing the listening
    CLICK is the mouse event that it is listening for (constants are written in all CAPS)
    onRotateLeft is the function being called that rotates the box 20 degrees to the left :
    function onRotateLeft(evt:MouseEvent):void {
    box.rotation -= 20;

top

Source:

This information and the SWF movie is drawn from the excellent book Learning ActionScript 3.0: A Beginner's Guide, by Rich Shupe with Zevan Rosser (2007). It is available in Safari books online through the Cal Poly Library.Download Chapter files for the ActionScript for this book that you can copy & adapt – licensed under Creative Commons License  http://examples.oreilly.com/9780596527877/

Note: "Ch3_" was added at beginning of SWF movie name because HTML validation requires that an id not start with a "0"

The SWF movie is displayed from the book site with permission by a Creative Commons License Attribution-Share Alike 2.5

top

Resources

top