Frontendplace Blog

Sharing Frontend developing ideas

dynamic attach movieclip by classname reference in AS3

Posted by info on December 24th, 2008

In actionscript3 it is possible to add a movieclip to the stage by the class reference. It’s not so obvious though

To add a movieClip on stage you have to use the addChild method. You can get the movieclip reference in actionscript3 when you have added a classname on the library item by the use of getDefinitionByName. Be sure you use the DisplayObject for referencing the returned movieclip

example:

package 
{
  	import flash.display.MovieClip;
    import flash.utils.getDefinitionByName;

    public class Background extends MovieClip
	{
		public function Background()
		{
			var _Class:Class = getDefinitionByName("backgroundmc") as Class;
			var a:MovieClip = new _Class();
			a.name = "a";
			addChild (a); 
		}
	}
}

example creating background movieclip:

package {
	
    import flash.display.DisplayObject;
    import flash.display.Sprite;

    public class myDocClass extends Sprite {

        public function myDocClass(){
        	 
			var a:DisplayObject= new Background();
			addChild (a); 
			
		}
		
    }
}