Solved

How do I duplicate movie clip and access it?

Posted on 2008-06-24
9
2,046 Views
Last Modified: 2012-06-27
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"); 

	};
 

	$newObject.addListener($listener);	
 

	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: " + _level1.bg._x);

};

Open in new window

0
Comment
Question by:NiklasMoller
  • 5
  • 2
  • 2
9 Comments
 
LVL 12

Expert Comment

by:SamuelRostol
Comment Utility
Try to change it to this:

[CODE]

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

Kindly,
Samuel
//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

0
 
LVL 34

Expert Comment

by:Aneesh Chopra
Comment Utility
For your Info,

MovieClip.duplicateMovieClip function does not duplicate the movieClip which loads external assets.
0
 
LVL 5

Author Comment

by:NiklasMoller
Comment Utility
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!
0
 
LVL 12

Expert Comment

by:SamuelRostol
Comment Utility
Aneesh:
Allways helpful to get your comments! Do you have any suggestions then? Must the external source be loaded again into the new movieclip?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 5

Author Comment

by:NiklasMoller
Comment Utility
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?
0
 
LVL 5

Author Comment

by:NiklasMoller
Comment Utility
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... :) ????
0
 
LVL 34

Accepted Solution

by:
Aneesh Chopra earned 500 total points
Comment Utility
Yes,
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..
0
 
LVL 5

Author Closing Comment

by:NiklasMoller
Comment Utility
Thanks! I understand now
0
 
LVL 5

Author Comment

by:NiklasMoller
Comment Utility
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!
cheers!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Read about why website design really matters in today's demanding market.
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now