Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Instantiating Objects

Instantiating an object involves constructing an instance of a class as an object using ActionScript. Whether the object will be used for display purposes or for managing data, objects are always instantiated the same way:

var objectName:ClassType =
  new ClassType();

To instantiate an object:

  • Add the following ActionScript after the import statements from the previous example to construct an object of the Shape class called square:

    var square:Shape = new Shape();

    Leave this file open for the next set of instructions.

Object methods

As discussed in Chapter 3, methods are functions that are defined in or written inside a class. Methods of a class are actions that an object can perform. They can accept arguments and return values if required. Methods of an object of a class can be called after the object has been instantiated.

To call methods of an object:

  1. Return to the file from the previous set of instructions.
  2. Add the following code, which will draw and fill the rectangle using methods of the Graphics class (which are inherited in the Shape object) after the square constructor method:

    square.graphics.beginFill(0xffcc00);
    square.graphics.lineStyle
      (2, 0x000000);
    square.graphics.drawRect
      (0, 0, 200, 200);
    square.graphics.endFill();
  3. Add the instance of square to the display list (make it viewable on the stage) using the addChild() method of the DisplayObject class at the end of the script:

    addChild(square);
  4. Preview your movie; you should see a 200 × 200 orange square drawn on the stage.

    Leave this file open for the next task.

Object properties

Properties are variables of an instance. They store physical and logical attributes of the object such as location (x and y position), size (width, height, scaleX, scaleY), and various values for behind-the-scenes use.

To set properties of an object:

  1. Return to the file from the previous task.
  2. Add the following ActionScript to the end of the code to set the x and y properties of the square to x 200 and y 50:

    square.x = 200;
    square.y = 50;
  3. Set the alpha property of square to .5 (50 percent of its full opacity):

    square.alpha = .5;
  4. Set the scaleX (horizontal) and scaleY (vertical) properties of square:

    square.scaleX = 1.5;
    square.scaleY = .5;

    As with the alpha property, the values for setting the scale properties are decimal based. 1 equals 100 percent, 1.5 equals 150 percent, .50 equals 50 percent, and so on.

  5. Set the rotation property of square to 45 (%):

    square.rotation = 45;
  6. Preview your file. The position (x, y), transparency (alpha), scale (scaleX, scaleY), and rotation of square should all be changed from the original property values.

Data-typing object instances

Just like variables, object instance variables need to be data typed. This practice prevents unintentionally instantiating a variable as a wrong object type, which produces a compile error.

To data-type an object instance:

  1. Add the following code to import the MovieClip class and create a variable named myMC. myMC is data-typed as an object of the MovieClip class:

    import flash.display.MovieClip;
    var myMC:MovieClip;
  2. To instantiate the object, use the new MovieClip() constructor method of myMC:

    myMC = new MovieClip();
  3. Preview your movie; you won’t see anything, but you should not get a compile error either.
  4. To create a compile error, try using the new Shape() constructor method:

    myMC = new Shape();

    Because myMC is data-typed as a MovieClip, using this constructor method should cause a compile error (Figure 4.2).

    Figure 4.2

    Figure 4.2 A compile error is produced when the instance data type and its class constructor method are mismatched.

  • + Share This
  • 🔖 Save To Your Account