Tim’s
Bandwagon

A first look at ActionScript 2

Macromedia announced Flash MX 2004 today (due for release sometime in September 2003, I’m not sure what the MX bit stands for).

ActionScript has been bumped-up a major revision to version 2. Here’s what I could find on Macromedia’s site about ActionScript 2.0. From the New Features page:

Take advantage of ActionScript 2.0’s more robust programming model and object-oriented programming support, which makes it more familiar to experienced Java programmers.

And from a more detailed Feature Tour:

With object-oriented programming support and a more robust, European Computer Manufacturers Association (ECMA) standards-compliant programming model, ActionScript 2.0 is more familiar to experienced Java programmers. Compile ActionScript 2.0 code to ActionScript 1.0 for playback on earlier versions of the Macromedia Flash Player.

I think it’s a bit light on details ;-) . Luckily, Colin Mook has posted an interesting preview of a sample ActionScript 2.0 class:

/**
 * A sample ActionScript 2.0 class.
 */

class Box {

  // Box dimensions.
  private var width:Number;
  private var height:Number;

  // Movie clip that will contain visual 
  // representation of the box.
  private var container_mc:MovieClip;

  /**
   * Constructor.
   */
  public function Box (w:Number, h:Number, 
                       x:Number, y:Number, 
                       target:MovieClip, depth:Number) {

    // Create the container clip that will hold Box visuals.
    container_mc = target.createEmptyMovieClip("boxcontainer" + depth, depth);

    // Initialize size.
    setWidth(w);
    setHeight(h);    

    // Initialize position.
    setX(x);
    setY(y);
  }

  /**
   * Accessor to retrieve width.
   */
  public function getWidth ():Number {
    return width;
  }

  /**
   * Accessor to assign width. This version both assigns the new width property
   * value and redraws the box based on the new width.
   */
  public function setWidth (w:Number):Void {
    width = w;
    draw();
  }

  /**
   * Accessor to retrieve height.
   */
  public function getHeight ():Number {
    return height;
  }

  /**
   * Accessor to assign height. This version both assigns the new height property
   * value and redraws the box based on the new height.
   */
  public function setHeight (h:Number):Void {
    height = h;
    draw();
  }

  /**
   * Accessor to retrieve x. For convenience, the x and y coordinates 
   * are stored directly on the container movie clip. If numeric accuracy 
   * were a concern, we'd store x as a Box property separately so
   * that it wouldn't be rounded by the MovieClip class.
   */
  public function getX ():Number {
    return container_mc._x;
  }

  /**
   * Accessor to assign x.
   */
  public function setX (x:Number):Void {
    container_mc._x = x;
  }

  /**
   * Accessor to retrieve y.
   */
  public function getY ():Number {
    return container_mc._y;
  }
 
  /**
   * Accessor to assign y.
   */
  public function setY (y:Number):Void {
    container_mc._y = y;
  }

  /**
   * Displays the Box instance on screen. Uses the MovieClip drawing methods to
   * draw lines in container_mc. For more information, see
   * ActionScript for Flash MX: The Definitive Guide.
   */
  public function draw ():Void {
    // Clear the previous box rendering.
    container_mc.clear();
    // Use a 1 point black line.
    container_mc.lineStyle(1, 0x000000);
    // Position the drawing pen.
    container_mc.moveTo(0, 0);
    // Start a white fill.
    container_mc.beginFill(0xFFFFFF, 100);
    // Draw the border of the box.
    container_mc.lineTo(width, 0);
    container_mc.lineTo(width, height);
    container_mc.lineTo(0, height);
    container_mc.lineTo(0, 0);
    // Formally stop filling the shape.
    container_mc.endFill();
  }
}

One Comment

  1. Hats off to Macromedia for having implemented this form of full fledged object oriented approach which is so familiar to all programmers makes life so much more easy to work…

Leave a Comment

Your personal information

Your comment