In the event of making new children, especially like library movieclips, you could create a generic function that receives the class reference for the movieclip you wish to create. For the below example, let’s say in your library is a RedBox linked movieclip with a variety of other movieclips like it that all need to be created the same way:

  1. addChild( create(RedBox) );
  2.  
  3. function create(ObjectType:Class):MovieClip{
  4.   // Adding "as MovieClip" tells Flash that ObjectType will be
  5.  // some kind of MovieClip derived class.
  6.   var newMC:MovieClip = new ObjectType() as MovieClip;
  7.   // Add new event listeners to newMC
  8.   // Add logic or variable settings like X and Y
  9.   // returns the newMC to be added to the stage.
  10.   return newMC;
  11. }

The benefit is you can dynamically create objects that all get setup the same way… the function does not care what type of movieclip needs to be created just as long as it’s a type of movieclip.

Auto-Generated Related Posts:
  1. Flash TOTD: inline functions...
  2. Flash - Working with Arrays...
  3. Flash Tutorial: Flash Stage Size...

Tags: ,