flash native_classes_dynamic_class

The dynamic class

As we have seen in previous articles, in ActionScript 3 there are two types of classes: dynamic and non-dynamic. In the case of graphic sub-classes, no matter what type of super-class is used the graphic sub-class is always non-dynamic by default. It is therefore impossible at this stage to add a property or a method upon execution to an instance of the pencil class (developed in the preceding articles):

// creation of the symbol
var myPencil:Pencil = new Pencil( .1 );
// addition of property on execution
myPencil.myProp = 12;

The previous code generates the following code on compilation:

1119: Access of possibly undefined property myProp through a reference with static type Pencil.

This default behaviour is good as it is generally advised against resorting to dynamic classes. By allowing the modification of a class’ implementation upon execution, the developer using the class has to browse the code in order to discover the behaviours that can be added on execution. By reading the class he is not informed of its capabilities and characteristics.

In rare situations it can be necessary to make the graphic sub-class dynamic. To do this we add the dynamic attribute before the class keyword:

dynamic public class Pencil extends MovieClip

Once the class is modified the following code works:

// creation of the symbol
var myPencil:Pencil = new Pencil( .1 );
// addition of property on execution
myPencil.myProp = 12;
// displays : 12
trace( myPencil.myProp);

Although the dynamic class exists, only the MovieClip class benefits within ActionScript 3, all of the other classes don’t allow the addition of properties or methods on execution are said to be non-dynamic.

It is strongly advised against using dynamic classes in your projects. By making a class dynamic no verification of the code on compilation can be done, we can therefore access private properties even if we get an undefined:

// creation of the symbol
var myPencil:Pencil = new Pencil( .1 );
// no verification of the compilation type
// happily, the virtual machine (VM2) keeps the types on execution
// and prevents its access by returning undefined
// displays : undefined
trace( myPencil.nFriction );

The virtual machine 2 (VM2) keeps the types on execution, when we access a private property in a dynamic class it returns undefined. In the case of a private method, a TypeError execution error is raised.

Worth remembering

  • The dynamic attribute allows you to make a class dynamic.
  • The verification of the type is done on compilation of the dynamic class.
  • It is advised against using dynamic classes in ActionScript projects.
  • In the case of graphic sub-classes, no matter what the type of super-class, the graphic sub-class is always dynamic by default.







Adobe Training center


Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42