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:

addChild( create(RedBox) );

function create(ObjectType:Class):MovieClip{
  // Adding "as MovieClip" tells Flash that ObjectType will be
 // some kind of MovieClip derived class.
  var newMC:MovieClip = new ObjectType() as MovieClip;
  // Add new event listeners to newMC
  // Add logic or variable settings like X and Y
  // returns the newMC to be added to the stage.
  return newMC;
}

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.


Author: Jonathan Dunlap
Jonathan is a veteran software architect, author of IsoHill, humanitarian, and has worked with Bigpoint Inc, CrowdStar, ePrize, and Microsoft.

Tags: ,