Return to Kroll Design home page

Flash Tutorials (with .FLA files included)

 
« Return

ActionScript 2

attachMovie for creating new movie clips on stage

Download the FLA file


You can use the attachMovie method to create new instances of a movie clip. You can attach the new clip to an existing movie clip (often an invisible or 'empty' container clip, though this is not required), or you can attach one or more new clips directly to the stage, which is called _root. To create new instances of a given clip, make sure you give it a linkage identifier by right clicking on the clip in the libraries panel and assigning a linkage identifier.

To remove movie clips from the stage, use the removeMovieClip method as shown in the code.

// RED BUTTON:
myButton1_btn.onRelease = function() {	
	empty_clip_mc._x = (Math.random() * 300) + 100;
	empty_clip_mc._y = (Math.random() * 200) + 100;	
	empty_clip_mc.attachMovie("myLinkageName","another_clip_mc",1);	
}

//GREEN BUTTON:
var instanceNumber:Number = 0;
myButton2_btn.onRelease = function() {	
	instanceNumber++;	
	newClip = _root.attachMovie("myLinkageName", "another_clip" + instanceNumber, instanceNumber);
	newClip._x = (Math.random() * 300) + 100;
	newClip._y = (Math.random() * 200) + 100;
	newClip._alpha = 40;
}

//REMOVE CLONED MOVIE CLIPS OFF STAGE:
myButton3_btn.onRelease = function() {
	removeMovieClips(_root);
	empty_clip_mc._x = 400;
	empty_clip_mc._y = 230;
}

function removeMovieClips(parent:MovieClip):Void
{	
  var i;
  for(i in parent)
	{ if(parent[i].removeMovieClip())
  { parent[i].removeMovieClip(); }
  }
}


 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM