<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Cool addChild trick! Why don&#8217;t people use the return of addChild?</title>
	<atom:link href="http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/</link>
	<description>At the top of our game in st.Louis</description>
	<lastBuildDate>Fri, 27 Aug 2010 21:31:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Thiago</title>
		<link>http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/comment-page-1/#comment-880</link>
		<dc:creator>Thiago</dc:creator>
		<pubDate>Thu, 27 May 2010 17:24:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3apex.com/?p=38#comment-880</guid>
		<description>I do the thing like that:

// storage
var a:Array = new Array()
// temp shape
var p:Shape = new Shape()

// reference for graphics
var g:Graphics = p.graphics
with(g)
{
lineStyle(1,0)
drawRect(0,0,10,10)
}

// one rect was been draw.. now just add it to the stage
stage.addChild(p)
// and the array
a[0] = p

// now create another rect
p = new Shape()
g = p.graphics
with(g){lineStyle(1,0); drawRect(20,20,10,10)}

// add it to stage, and storage
stage.addChild(p)
a[1] = p

/// 
then, a[0] = first rect;
a[1] = second rect
and i dont have a single reference var for each Shape

/////
here&#039;s a cool example of mine :) with source, of cource 
(Simple particles 2 (100k))
http://wonderfl.net/c/A4dj</description>
		<content:encoded><![CDATA[<p>I do the thing like that:</p>
<p>// storage<br />
var a:Array = new Array()<br />
// temp shape<br />
var p:Shape = new Shape()</p>
<p>// reference for graphics<br />
var g:Graphics = p.graphics<br />
with(g)<br />
{<br />
lineStyle(1,0)<br />
drawRect(0,0,10,10)<br />
}</p>
<p>// one rect was been draw.. now just add it to the stage<br />
stage.addChild(p)<br />
// and the array<br />
a[0] = p</p>
<p>// now create another rect<br />
p = new Shape()<br />
g = p.graphics<br />
with(g){lineStyle(1,0); drawRect(20,20,10,10)}</p>
<p>// add it to stage, and storage<br />
stage.addChild(p)<br />
a[1] = p</p>
<p>///<br />
then, a[0] = first rect;<br />
a[1] = second rect<br />
and i dont have a single reference var for each Shape</p>
<p>/////<br />
here&#8217;s a cool example of mine <img src='http://www.as3apex.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  with source, of cource<br />
(Simple particles 2 (100k))<br />
<a href="http://wonderfl.net/c/A4dj" rel="nofollow">http://wonderfl.net/c/A4dj</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: IK</title>
		<link>http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/comment-page-1/#comment-861</link>
		<dc:creator>IK</dc:creator>
		<pubDate>Fri, 23 Apr 2010 16:02:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3apex.com/?p=38#comment-861</guid>
		<description>Agreed in principle. But i do like this method

var badies:Arrary = new Array()
badies.push ( addChild ( new Sprite() as Sprite ) )

To me this doesn&#039;t look to bad and I like the idea of using the returned data at the time of inception instead of having flash lookup the data twice ( shortest route principle ).

Those tests above are quite expected in fact as there is a table lookup factor for method 2 and every loop there is 1 extra added to the table as well as a new resource being allocated.</description>
		<content:encoded><![CDATA[<p>Agreed in principle. But i do like this method</p>
<p>var badies:Arrary = new Array()<br />
badies.push ( addChild ( new Sprite() as Sprite ) )</p>
<p>To me this doesn&#8217;t look to bad and I like the idea of using the returned data at the time of inception instead of having flash lookup the data twice ( shortest route principle ).</p>
<p>Those tests above are quite expected in fact as there is a table lookup factor for method 2 and every loop there is 1 extra added to the table as well as a new resource being allocated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AJ</title>
		<link>http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/comment-page-1/#comment-728</link>
		<dc:creator>AJ</dc:creator>
		<pubDate>Wed, 03 Feb 2010 11:37:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3apex.com/?p=38#comment-728</guid>
		<description>Just stumbled on this while trying to find some info on some weird super.method functioning I&#039;m getting with Flex.  Thought I&#039;d give you this bit of code, since I&#039;m here:

( s = addChild(new Sprite()) as Sprite ).x = 100;

Looks ugly, so probably shouldn&#039;t be used unless you&#039;re *REALLY* dependant on every CPU cycle, but there might be a situation where the concept could be useful to someone.</description>
		<content:encoded><![CDATA[<p>Just stumbled on this while trying to find some info on some weird super.method functioning I&#8217;m getting with Flex.  Thought I&#8217;d give you this bit of code, since I&#8217;m here:</p>
<p>( s = addChild(new Sprite()) as Sprite ).x = 100;</p>
<p>Looks ugly, so probably shouldn&#8217;t be used unless you&#8217;re *REALLY* dependant on every CPU cycle, but there might be a situation where the concept could be useful to someone.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jon</title>
		<link>http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/comment-page-1/#comment-684</link>
		<dc:creator>jon</dc:creator>
		<pubDate>Thu, 14 Jan 2010 15:19:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3apex.com/?p=38#comment-684</guid>
		<description>also check this out... end up looking like:

var s:Sprite = Sprite( mainSprite.addChild(create(Class, {x:0, y:0, rotation:90})));

// Instantiate and Set Properties
function create(obj:Class, props:Object):*
{
	var o:* = new obj();
	for (var p:String in props){
		o[p] = props[p];
	}
	return o;
}</description>
		<content:encoded><![CDATA[<p>also check this out&#8230; end up looking like:</p>
<p>var s:Sprite = Sprite( mainSprite.addChild(create(Class, {x:0, y:0, rotation:90})));</p>
<p>// Instantiate and Set Properties<br />
function create(obj:Class, props:Object):*<br />
{<br />
	var o:* = new obj();<br />
	for (var p:String in props){<br />
		o[p] = props[p];<br />
	}<br />
	return o;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: spender</title>
		<link>http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/comment-page-1/#comment-98</link>
		<dc:creator>spender</dc:creator>
		<pubDate>Thu, 04 Sep 2008 22:45:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3apex.com/?p=38#comment-98</guid>
		<description>I know constructs like this:

array.push(addChild(_mc))

and this:

var s:Sprite = addChild(new Sprite);
s.x = 100;

look cool, but unless there&#039;s a real speed benefit in a portion of code that needs optimization, I think it&#039;s better to keep things separated. Ultimately, the code is more readable this way which carries a far greater benefit than a snappy way of doing 2 things in a single line.</description>
		<content:encoded><![CDATA[<p>I know constructs like this:</p>
<p>array.push(addChild(_mc))</p>
<p>and this:</p>
<p>var s:Sprite = addChild(new Sprite);<br />
s.x = 100;</p>
<p>look cool, but unless there&#8217;s a real speed benefit in a portion of code that needs optimization, I think it&#8217;s better to keep things separated. Ultimately, the code is more readable this way which carries a far greater benefit than a snappy way of doing 2 things in a single line.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aestheticsData</title>
		<link>http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/comment-page-1/#comment-82</link>
		<dc:creator>aestheticsData</dc:creator>
		<pubDate>Wed, 27 Aug 2008 09:37:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3apex.com/?p=38#comment-82</guid>
		<description>here is some performance test

1st method:
----------
for(var i:int = 0 ; i &lt; NB ; i += 1) {
	var s:Sprite = addChild( new Sprite ) as Sprite;
}


2nd method:
-----------
for( var k:int = 0 ; k &lt; NB ; k += 1) {

	var s2:Sprite = new Sprite;
	addChild( s2 );
}

average time:
---------------------------------------
NB              &#124; 0.5K  1K   2K   4K   6K   8K   10K   12K
2nd / 1st ratio &#124; 1.3   1.6  2.1  2.7  5    9.3  9.4   9.4

we can see that when there is fewer than 500 Sprite both methods are almost equivalent. But beyond 2K Sprites 1st method is a lot more shorther than 2nd. And around 8K and beyond the difference is big, but stable when NB increase over 8K</description>
		<content:encoded><![CDATA[<p>here is some performance test</p>
<p>1st method:<br />
&#8212;&#8212;&#8212;-<br />
for(var i:int = 0 ; i &lt; NB ; i += 1) {<br />
	var s:Sprite = addChild( new Sprite ) as Sprite;<br />
}</p>
<p>2nd method:<br />
&#8212;&#8212;&#8212;&#8211;<br />
for( var k:int = 0 ; k &lt; NB ; k += 1) {</p>
<p>	var s2:Sprite = new Sprite;<br />
	addChild( s2 );<br />
}</p>
<p>average time:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
NB              | 0.5K  1K   2K   4K   6K   8K   10K   12K<br />
2nd / 1st ratio | 1.3   1.6  2.1  2.7  5    9.3  9.4   9.4</p>
<p>we can see that when there is fewer than 500 Sprite both methods are almost equivalent. But beyond 2K Sprites 1st method is a lot more shorther than 2nd. And around 8K and beyond the difference is big, but stable when NB increase over 8K</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: aestheticsData</title>
		<link>http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/comment-page-1/#comment-81</link>
		<dc:creator>aestheticsData</dc:creator>
		<pubDate>Wed, 27 Aug 2008 09:00:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3apex.com/?p=38#comment-81</guid>
		<description>&quot; var s:Sprite = addChild( new Sprite );
  s.x = 100; &quot;

it doesn&#039;t work. addChild returns a DisplayObject and not a Sprite. So to make it work :

var s:Sprite = addChild( new Sprite ) as Sprite;</description>
		<content:encoded><![CDATA[<p>&#8221; var s:Sprite = addChild( new Sprite );<br />
  s.x = 100; &#8221;</p>
<p>it doesn&#8217;t work. addChild returns a DisplayObject and not a Sprite. So to make it work :</p>
<p>var s:Sprite = addChild( new Sprite ) as Sprite;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Chernoff</title>
		<link>http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/comment-page-1/#comment-78</link>
		<dc:creator>Josh Chernoff</dc:creator>
		<pubDate>Tue, 19 Aug 2008 15:42:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3apex.com/?p=38#comment-78</guid>
		<description>well here is a small example.

var someArray:Array = new Array();
function someFunction():void{
     //make a bunch of sprites
     var sprite:Sprite = new Sprite();
     someArray.push(addChild(sprite));
}
//now what if I want to access the sprites?
//Do I use the array or getChildAt()?

removeChild(someArray[someIndex]);
vs.
removeChild(getChildByName(&quot;hadINamedItThisMightwork&quot;));
vs.
removeChildAt(getChildAt(someIndex)); 
//if I know the index of the child then this might work.

get the point yet?</description>
		<content:encoded><![CDATA[<p>well here is a small example.</p>
<p>var someArray:Array = new Array();<br />
function someFunction():void{<br />
     //make a bunch of sprites<br />
     var sprite:Sprite = new Sprite();<br />
     someArray.push(addChild(sprite));<br />
}<br />
//now what if I want to access the sprites?<br />
//Do I use the array or getChildAt()?</p>
<p>removeChild(someArray[someIndex]);<br />
vs.<br />
removeChild(getChildByName(&#8220;hadINamedItThisMightwork&#8221;));<br />
vs.<br />
removeChildAt(getChildAt(someIndex));<br />
//if I know the index of the child then this might work.</p>
<p>get the point yet?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: radekg</title>
		<link>http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/comment-page-1/#comment-77</link>
		<dc:creator>radekg</dc:creator>
		<pubDate>Tue, 19 Aug 2008 15:15:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3apex.com/?p=38#comment-77</guid>
		<description>What is the point of storing child items in separate array apart of making GC confused? If child items are in its parent why to access them with separate array?</description>
		<content:encoded><![CDATA[<p>What is the point of storing child items in separate array apart of making GC confused? If child items are in its parent why to access them with separate array?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martha</title>
		<link>http://www.as3apex.com/as3/cool-addchild-trick-why-dont-people-not-use-the-return-of-addchild/comment-page-1/#comment-68</link>
		<dc:creator>Martha</dc:creator>
		<pubDate>Thu, 14 Aug 2008 17:06:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3apex.com/?p=38#comment-68</guid>
		<description>Nice!

I thought I was all alone. I&#039;ve been routinely gathering all my children generated in loops into an easy to grab array like this:

addChild(art);
artList.push(art);

I like your system -- short and to the point.</description>
		<content:encoded><![CDATA[<p>Nice!</p>
<p>I thought I was all alone. I&#8217;ve been routinely gathering all my children generated in loops into an easy to grab array like this:</p>
<p>addChild(art);<br />
artList.push(art);</p>
<p>I like your system &#8212; short and to the point.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
