How do I duplicate movie clip and access it?

I have a problem which probably is related to levels or depth in flash, but I just cant work it out...
Basically I have just created a movieclip called "bg_mc". It shows up nicely on my stage, jpg loaded into it and all. so far so good.

I want to duplicate it and then access the new (just duplicated) movieclip. it doesnt work.

I am using the movieClipLoader class to do the loading work for me.
I have assigned some code to a button to find out if the movieclip exists or not.
The duplicated movieclip should exist, but it doesnt (when i press the button, the trace says:  "undefined" when I ask for the ._x position of the duplicated movieclip.)

The dynamic loading of the jpg and the loadMovieClip class function works fine (or so I think... hehe) but the duplicateMovieClip doesnt work... what am I missing here?

Heres my code:

function createMovieClipLoaderObject($newObjectName){
	//Just store a copy of the name of the object into a new variable:
	var $newObject = $newObjectName;	
	//Convert it to an object:
	var $newObject:MovieClipLoader = new MovieClipLoader();
	//create a unique name for this listener:
	var $listener_name = $newObjectName+"_listener";
	//save the new name:
	var $listener = $listener_name;
	//keep the name, but convert it to an object:
	var $listener:Object = new Object();
	$listener.onLoadStart = function(target_mc:MovieClip) {
	   trace("*********"+$newObjectName+" instance*********");
	   trace("onLoadStart event triggered:");
	   trace("Your load has begun on movie clip = "+target_mc);
	   var loadProgress:Object = $newObject.getProgress(target_mc);
	   trace(loadProgress.bytesLoaded+" = bytes loaded at start");
	   trace(loadProgress.bytesTotal+" = bytes total at start");
	$listener.onLoadProgress = function(target_mc:MovieClip, loadedBytes:Number, totalBytes:Number) {
	   trace("*********"+$newObjectName+" instance Progress*********");
	   trace("onLoadProgress event triggered:");
	   trace("onLoadProgress() called back on movie clip "+target_mc);
	   trace(loadedBytes+" = bytes loaded at progress callback");
	   trace(totalBytes+" = bytes total at progress callback");
	$listener.onLoadComplete = function(target_mc:MovieClip) {
	   trace("*********"+$newObjectName+" instance*********");
	   trace("onLoadComplete event triggered:");
	   trace("Your load is done on movie clip = "+target_mc);
	   var loadProgress:Object = $newObject.getProgress(target_mc);
	   trace(loadProgress.bytesLoaded+" = bytes loaded at end");
	   trace(loadProgress.bytesTotal+" = bytes total at end");
	$listener.onLoadInit = function(target_mc:MovieClip) {
	   trace("*********"+$newObjectName+" instance*********");
	   trace("onLoadInit event triggered:");
	   trace("Movie clip = "+target_mc+" is now initialized");
	   // you can now do any setup required, for example:
	   //trace("width of image is: "+target_mc._width);
	   //trace("height of image is: "+target_mc._height);
	$listener.onLoadError = function(target_mc:MovieClip, errorCode:String) {
	   trace("*********"+$newObjectName+" instance*********");
	   trace("onLoadError event triggered:");
	   trace("ERROR CODE = "+errorCode);
	   trace("Your load failed on movie clip = "+target_mc+"\n"); 
	return $newObject;
};// end function create object
//create new object to handle the loading:
var $my_test = createMovieClipLoaderObject("my_test");
//create empty movie clip to use as container for jpg:
_root.createEmptyMovieClip("bg_mc", _root.getNextHighestDepth());
// Now load the file into target mc
$my_test.loadClip("tile1.jpg", _root.bg_mc);
//when the file is completely loaded, duplicate the mc:
$my_test.onLoadInit = function(target_mc:MovieClip){
	duplicateMovieClip(target_mc, "bg", _root.getNextHighestDepth()); 
};//end onLoad init
Actionscript for testing button:
testbutton.onRelease = function(){
	trace("duplicate x is: " +;

Open in new window

Who is Participating?
Aneesh ChopraConnect With a Mentor Commented:
In Flash authoring environment, simulation actually downloads each instance seperately again n again..
but in real scenario, image will get loaded once and rest of the instnaces will loads it from the browser cache for sure..
Try to change it to this:


Any changes? And, ActionScript does per default not use $ in front of variables, so you can remove them if you wish :)

//when the file is completely loaded, duplicate the mc:
var $duplicate :MovieClip;
$my_test.onLoadComplete = function(target_mc:MovieClip){
	$duplicate = duplicateMovieClip(target_mc, "bg", _root.getNextHighestDepth()); 
};//end onLoad init
Actionscript for testing button:
testbutton.onRelease = function(){
	trace("duplicate x is: " + _parent.duplicate._x);

Open in new window

Aneesh ChopraCommented:
For your Info,

MovieClip.duplicateMovieClip function does not duplicate the movieClip which loads external assets.
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

NiklasMollerAuthor Commented:
Thanks for the proposal, but I cant get that to work either...

I did some more research and it seems to be not possible to duplicate a movieclip that has been the target of a dynamically loaded jpg.... (dont know if I am correct on that info. but thats how i understand it)
In any case the jpg loaded into the mc will NOT be duplicated also, and that is what I wanted to acheive.
So I am loading the jpg for every new mc I create instead. Not sure how that will work out "live" but its my only solution so far.

thanks anyway!
Allways helpful to get your comments! Do you have any suggestions then? Must the external source be loaded again into the new movieclip?
NiklasMollerAuthor Commented:
to aneeshchopra:

Do you know of a better way to copy an already dynamically loaded jpg and spread it across the stage (so that I dont have to load it one more time every time I want to use the same image) ??
I am making a function that will tile images across the background of my flash stage.

the way I do it now is that I create a new movie clip for each image file, and then I dynamically load the SAME jpg into that movie clip. But I have to do it like 170 times to cover the stage. It works, but is there a more effective way?
NiklasMollerAuthor Commented:
and also... :)

do you know if the browser cache will help, so that the image file is actually only loaded to the browser computer ONCE, but accessed 170 times when requested by the flash movie???

that would improve the situation for me, because then the jpg file will only load ONCE in reality, which will be alot faster.
However, when I choose "simulate download" in the Flash authoring environment, then it seems like the flash file is simulating the same download time of the jpg EVERY time I load it.
= meaning it does NOT simulate the browser cache storing the jpg

im confused... :) ????
NiklasMollerAuthor Commented:
Thanks! I understand now
NiklasMollerAuthor Commented:
Okay. so then copying the clip and loading the SAME jpg over and over again actually will only mean loading the image into cache ONCE, and then accessing the file from there. thats good to know!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.