<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Duplicating Movie Clips Completely</title>
	<link>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely</link>
	<description>Luck is the residue of good design.</description>
	<pubDate>Wed, 19 Nov 2008 04:09:38 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>

	<item>
		<title>by: Tiemen</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-20004</link>
		<pubDate>Thu, 22 Feb 2007 11:38:09 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-20004</guid>
					<description>I take that back. Flash just uses the browser cache as library, so though flash takes time to load the images from the cache into flash, it does not load the images twice. Splendid.</description>
		<content:encoded><![CDATA[<p>I take that back. Flash just uses the browser cache as library, so though flash takes time to load the images from the cache into flash, it does not load the images twice. Splendid.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Tiemen</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-19998</link>
		<pubDate>Thu, 22 Feb 2007 10:42:31 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-19998</guid>
					<description>Is there by any chance another way to re-use images that have been loaded? What you'd really want is a loader that puts the loaded images into the 'library', so that from the moment they've been loaded they can be accessed freely by any function inside the movieclip. 

I'm currently creating a movie that has a real time reflection of the images loaded. Loading the images twice feels incredibly stupid and costs a load of bandwidth...

:-/</description>
		<content:encoded><![CDATA[<p>Is there by any chance another way to re-use images that have been loaded? What you&#8217;d really want is a loader that puts the loaded images into the &#8216;library&#8217;, so that from the moment they&#8217;ve been loaded they can be accessed freely by any function inside the movieclip. </p>
<p>I&#8217;m currently creating a movie that has a real time reflection of the images loaded. Loading the images twice feels incredibly stupid and costs a load of bandwidth&#8230;</p>
<p>:-/
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-19362</link>
		<pubDate>Mon, 19 Feb 2007 17:47:57 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-19362</guid>
					<description>Tiemen,

Wow, good catch!  ActionScript 3.0 &lt;em&gt;might&lt;/em&gt; have a way around this, but I believe you're right:  in AS2, loaded external content, such as a JPG, almost certainly would not &quot;clone&quot; in the sense discussed above.  I can't think of a way to check if external content has even &lt;em&gt;been&lt;/em&gt; loaded, so this may be the one final obstacle that keeps AS2 object cloning almost, but not quite, an achievable goal.</description>
		<content:encoded><![CDATA[<p>Tiemen,</p>
<p>Wow, good catch!  ActionScript 3.0 <em>might</em> have a way around this, but I believe you&#8217;re right:  in AS2, loaded external content, such as a JPG, almost certainly would not &#8220;clone&#8221; in the sense discussed above.  I can&#8217;t think of a way to check if external content has even <em>been</em> loaded, so this may be the one final obstacle that keeps AS2 object cloning almost, but not quite, an achievable goal.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Tiemen</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-19324</link>
		<pubDate>Mon, 19 Feb 2007 14:25:17 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-19324</guid>
					<description>(Since I'm into the MovieClipLoader at present) What about duplicating a movieclip which has an image loaded into it, completely? When you duplicate a movieclip which contains an image loaded by loadClip() this content won't duplicate with it.</description>
		<content:encoded><![CDATA[<p>(Since I&#8217;m into the MovieClipLoader at present) What about duplicating a movieclip which has an image loaded into it, completely? When you duplicate a movieclip which contains an image loaded by loadClip() this content won&#8217;t duplicate with it.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Movie Props</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-4089</link>
		<pubDate>Mon, 23 Oct 2006 01:44:27 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-4089</guid>
					<description>Hey cool, thanks for the code.</description>
		<content:encoded><![CDATA[<p>Hey cool, thanks for the code.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: NSurveyor</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-469</link>
		<pubDate>Thu, 22 Jun 2006 01:38:45 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-469</guid>
					<description>Alright, I think I figured it out for the most part... Use:

resetRefs();
for (var prop:String in target) {
	if(target._parent[newName][prop] == undefined){
		target._parent[newName][prop] = copyMe(target[prop]);
	}
}

My new code will keep &quot;pointers&quot; intact, e.g.:

resetRefs();
o = {};
o.a = new Date();
o.b = o.a;
o.c = new Date();
trace('a: '+o.a); // these 3
trace('b: '+o.b); // all show up
trace('c: '+o.c); // the same
p = copyMe(o);
p.a.setTime(0);
trace('a: '+p.a); // only this
trace('b: '+p.b); // and this are the same
trace('c: '+p.c); // this value didn't change

Here is the necessary code to make this all work:

Array.prototype.indexOf = function(x){
	for(var y in this){
		if(this[y] === x){
			return y;
		}
	}
	return -1;
};
function resetRefs(){
	_global.refs = [];
	_global.dups = [];
}
function copyMe(obj) {
	var n = {};
	var i = _global.refs.indexOf(obj);
	if(i!=-1){
		return _global.dups[i];
	}else{
		if (obj == undefined &amp;#124;&amp;#124; obj == null) {
			return obj;
		} else if (obj instanceof String &amp;#124;&amp;#124; typeof (obj) == 'string') {
			n = (obj === obj.valueOf()) ? obj : new String(obj.valueOf());
		} else if (obj instanceof Number &amp;#124;&amp;#124; typeof (obj) == 'number') {
			n = (obj === obj.valueOf()) ? obj : new Number(obj.valueOf());
		} else if (obj instanceof Boolean &amp;#124;&amp;#124; typeof (obj) == 'boolean') {
			n = (obj === obj.valueOf()) ? obj : new Boolean(obj.valueOf());
		} else if (obj instanceof Date) {
			n = new Date(obj.valueOf());
		} else if (obj instanceof Array) {
			n = obj.slice();
		} else if (obj instanceof Error) {
			n = new Error();
		}
		if (obj instanceof MovieClip &amp;#124;&amp;#124; obj instanceof Button &amp;#124;&amp;#124; obj instanceof TextField) {
			n = obj;
		} else {
			for (var x in obj) {
				n[x] = copyMe(obj[x]);
			}
		}
		_global.refs.push(obj);
		_global.dups.push(n);
		return n;
	}
}</description>
		<content:encoded><![CDATA[<p>Alright, I think I figured it out for the most part&#8230; Use:</p>
<p>resetRefs();<br />
for (var prop:String in target) {<br />
	if(target._parent[newName][prop] == undefined){<br />
		target._parent[newName][prop] = copyMe(target[prop]);<br />
	}<br />
}</p>
<p>My new code will keep &#8220;pointers&#8221; intact, e.g.:</p>
<p>resetRefs();<br />
o = {};<br />
o.a = new Date();<br />
o.b = o.a;<br />
o.c = new Date();<br />
trace(&#8217;a: &#8216;+o.a); // these 3<br />
trace(&#8217;b: &#8216;+o.b); // all show up<br />
trace(&#8217;c: &#8216;+o.c); // the same<br />
p = copyMe(o);<br />
p.a.setTime(0);<br />
trace(&#8217;a: &#8216;+p.a); // only this<br />
trace(&#8217;b: &#8216;+p.b); // and this are the same<br />
trace(&#8217;c: &#8216;+p.c); // this value didn&#8217;t change</p>
<p>Here is the necessary code to make this all work:</p>
<p>Array.prototype.indexOf = function(x){<br />
	for(var y in this){<br />
		if(this[y] === x){<br />
			return y;<br />
		}<br />
	}<br />
	return -1;<br />
};<br />
function resetRefs(){<br />
	_global.refs = [];<br />
	_global.dups = [];<br />
}<br />
function copyMe(obj) {<br />
	var n = {};<br />
	var i = _global.refs.indexOf(obj);<br />
	if(i!=-1){<br />
		return _global.dups[i];<br />
	}else{<br />
		if (obj == undefined || obj == null) {<br />
			return obj;<br />
		} else if (obj instanceof String || typeof (obj) == &#8217;string&#8217;) {<br />
			n = (obj === obj.valueOf()) ? obj : new String(obj.valueOf());<br />
		} else if (obj instanceof Number || typeof (obj) == &#8216;number&#8217;) {<br />
			n = (obj === obj.valueOf()) ? obj : new Number(obj.valueOf());<br />
		} else if (obj instanceof Boolean || typeof (obj) == &#8216;boolean&#8217;) {<br />
			n = (obj === obj.valueOf()) ? obj : new Boolean(obj.valueOf());<br />
		} else if (obj instanceof Date) {<br />
			n = new Date(obj.valueOf());<br />
		} else if (obj instanceof Array) {<br />
			n = obj.slice();<br />
		} else if (obj instanceof Error) {<br />
			n = new Error();<br />
		}<br />
		if (obj instanceof MovieClip || obj instanceof Button || obj instanceof TextField) {<br />
			n = obj;<br />
		} else {<br />
			for (var x in obj) {<br />
				n[x] = copyMe(obj[x]);<br />
			}<br />
		}<br />
		_global.refs.push(obj);<br />
		_global.dups.push(n);<br />
		return n;<br />
	}<br />
}
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: NSurveyor</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-131</link>
		<pubDate>Fri, 19 May 2006 02:06:16 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-131</guid>
					<description>I think I figured something out.... My code will only clone the core classes of ActionScript... but adding in more isn't that difficult if needed. 

To fix your code, just change the line in the for loop:

 target._parent[newName][prop] = copyMe(target[prop]);

The problem with copying is that when a property includes a reference to a clipC (say within clipA) and then you clone clipA to clipB. Should the reference to clipC be the one in clipA or the duplicated one in clipB?

Also, say clipA has a non primitive variable, A, and a pointer to it, B. When you clone, how will the code know that B should point to the duplicated A, and not make a clone of A and another of clone of A in B. My code will do the latter. Just like in real life, can cloning really be perfected???

Here's my copyMe function... (hasn't been tested completely though!)

function copyMe(obj) {
	var n = {};
	if(obj == undefined &amp;#124;&amp;#124; obj == null){
		return obj;
	}else if (obj instanceof String &amp;#124;&amp;#124; typeof (obj) == 'string') {
		n = (obj === obj.valueOf()) ? obj : new String(obj.valueOf());
	} else if (obj instanceof Number &amp;#124;&amp;#124; typeof (obj) == 'number') {
		n = (obj === obj.valueOf()) ? obj : new Number(obj.valueOf());
	} else if (obj instanceof Boolean &amp;#124;&amp;#124; typeof (obj) == 'boolean') {
		n = (obj === obj.valueOf()) ? obj : new Boolean(obj.valueOf());
	} else if (obj instanceof Date) {
		n = new Date(obj.valueOf());
	} else if (obj instanceof Array) {
		n = obj.slice();
	} else if (obj instanceof Error) {
		n = new Error();
	}
	if (obj instanceof MovieClip &amp;#124;&amp;#124; obj instanceof Button &amp;#124;&amp;#124; obj instanceof TextField) {
		n = obj;
	} else {
		for (var x in obj) {
			n[x] = copyMe(obj[x]);
		}
	}
	return n;
}</description>
		<content:encoded><![CDATA[<p>I think I figured something out&#8230;. My code will only clone the core classes of ActionScript&#8230; but adding in more isn&#8217;t that difficult if needed. </p>
<p>To fix your code, just change the line in the for loop:</p>
<p> target._parent[newName][prop] = copyMe(target[prop]);</p>
<p>The problem with copying is that when a property includes a reference to a clipC (say within clipA) and then you clone clipA to clipB. Should the reference to clipC be the one in clipA or the duplicated one in clipB?</p>
<p>Also, say clipA has a non primitive variable, A, and a pointer to it, B. When you clone, how will the code know that B should point to the duplicated A, and not make a clone of A and another of clone of A in B. My code will do the latter. Just like in real life, can cloning really be perfected???</p>
<p>Here&#8217;s my copyMe function&#8230; (hasn&#8217;t been tested completely though!)</p>
<p>function copyMe(obj) {<br />
	var n = {};<br />
	if(obj == undefined || obj == null){<br />
		return obj;<br />
	}else if (obj instanceof String || typeof (obj) == &#8217;string&#8217;) {<br />
		n = (obj === obj.valueOf()) ? obj : new String(obj.valueOf());<br />
	} else if (obj instanceof Number || typeof (obj) == &#8216;number&#8217;) {<br />
		n = (obj === obj.valueOf()) ? obj : new Number(obj.valueOf());<br />
	} else if (obj instanceof Boolean || typeof (obj) == &#8216;boolean&#8217;) {<br />
		n = (obj === obj.valueOf()) ? obj : new Boolean(obj.valueOf());<br />
	} else if (obj instanceof Date) {<br />
		n = new Date(obj.valueOf());<br />
	} else if (obj instanceof Array) {<br />
		n = obj.slice();<br />
	} else if (obj instanceof Error) {<br />
		n = new Error();<br />
	}<br />
	if (obj instanceof MovieClip || obj instanceof Button || obj instanceof TextField) {<br />
		n = obj;<br />
	} else {<br />
		for (var x in obj) {<br />
			n[x] = copyMe(obj[x]);<br />
		}<br />
	}<br />
	return n;<br />
}
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-24</link>
		<pubDate>Fri, 14 Apr 2006 17:58:52 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-24</guid>
					<description>Daniel,

You bring up a good point.  Sure enough, you're right -- and, of course, your passed-by-reference observation makes perfect sense.  That said, the &lt;code&gt;MovieClip&lt;/code&gt; instance is, I maintain, indeed cloned:  the original points to an object by reference (here, an array), and the clone points to the same object by reference.

Here again, what we mean by &quot;clone&quot; is kind of a cheat, isn't it?  We want a conceptual clone, not an actual one.  We want cloned movie clips with cloned composite (non-primitive) properties.

Your comment has me thinking.  If you figure something out, please post again.  :)  I'll do the same.</description>
		<content:encoded><![CDATA[<p>Daniel,</p>
<p>You bring up a good point.  Sure enough, you&#8217;re right &#8212; and, of course, your passed-by-reference observation makes perfect sense.  That said, the <code>MovieClip</code> instance is, I maintain, indeed cloned:  the original points to an object by reference (here, an array), and the clone points to the same object by reference.</p>
<p>Here again, what we mean by &#8220;clone&#8221; is kind of a cheat, isn&#8217;t it?  We want a conceptual clone, not an actual one.  We want cloned movie clips with cloned composite (non-primitive) properties.</p>
<p>Your comment has me thinking.  If you figure something out, please post again.  <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   I&#8217;ll do the same.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Daniel Larsen</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-23</link>
		<pubDate>Fri, 14 Apr 2006 16:13:59 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely#comment-23</guid>
					<description>time and time again i have revisited this issue....how to really completely CLONE a movieclip.  the method above is something that i often come up with.....however i have discovered a flaw in it....and it always confuses me until i figure it out again.  here it is:

ok, so we &quot;cloned&quot; it right?  i have discovered that the two are very much still linked together.  for instance....AFTER cloning, add in the line:
originalClip.souvenirs[1] = &quot;CHANGED&quot;;
then print them BOTH out.  you see that they BOTH changed.

now i have had enough programming to know what is going on here.  any variable is passed by reference unless it is a primitive data type (int, string, etc);  objects, arrays, movieclips, etc are all passed by reference.  in other words.....there is still only ONE &quot;souvenirs&quot; array, it is stored on the originalClip and the newClip has a pointer to that.

so my question is this.....how do we REALLY clone something?  still has yet to be determined.....i have played around with some pretty intense recursive functions that check to see if it is a primitive type or not, if it is not then it needs to create a new object, and go recursively copy all of it's primitives as well.  i don't know if this makes sense, and i haven't really done it in depthly yet.

i will post the solution if i ever have the time to figure it out, in the mean time....anyone else?  what do you think David?</description>
		<content:encoded><![CDATA[<p>time and time again i have revisited this issue&#8230;.how to really completely CLONE a movieclip.  the method above is something that i often come up with&#8230;..however i have discovered a flaw in it&#8230;.and it always confuses me until i figure it out again.  here it is:</p>
<p>ok, so we &#8220;cloned&#8221; it right?  i have discovered that the two are very much still linked together.  for instance&#8230;.AFTER cloning, add in the line:<br />
originalClip.souvenirs[1] = &#8220;CHANGED&#8221;;<br />
then print them BOTH out.  you see that they BOTH changed.</p>
<p>now i have had enough programming to know what is going on here.  any variable is passed by reference unless it is a primitive data type (int, string, etc);  objects, arrays, movieclips, etc are all passed by reference.  in other words&#8230;..there is still only ONE &#8220;souvenirs&#8221; array, it is stored on the originalClip and the newClip has a pointer to that.</p>
<p>so my question is this&#8230;..how do we REALLY clone something?  still has yet to be determined&#8230;..i have played around with some pretty intense recursive functions that check to see if it is a primitive type or not, if it is not then it needs to create a new object, and go recursively copy all of it&#8217;s primitives as well.  i don&#8217;t know if this makes sense, and i haven&#8217;t really done it in depthly yet.</p>
<p>i will post the solution if i ever have the time to figure it out, in the mean time&#8230;.anyone else?  what do you think David?
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
