Solved

How do I duplicate movie clip and access it?

Posted on 2008-06-24
9
2,066 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 12

Expert Comment

by:SamuelRostol
ID: 21854313
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
ID: 21855017
For your Info,

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

Author Comment

by:NiklasMoller
ID: 21855102
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 12

Expert Comment

by:SamuelRostol
ID: 21855141
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
 
LVL 5

Author Comment

by:NiklasMoller
ID: 21855147
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
ID: 21855236
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
ID: 21855540
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
ID: 31470085
Thanks! I understand now
0
 
LVL 5

Author Comment

by:NiklasMoller
ID: 21856261
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…

705 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