<?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: How to Reference Objects Dynamically</title>
	<link>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically</link>
	<description>Luck is the residue of good design.</description>
	<pubDate>Wed, 19 Nov 2008 03:39:45 +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/reference-objects-dynamically#comment-179707</link>
		<pubDate>Thu, 08 May 2008 15:04:52 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-179707</guid>
					<description>Thanks for the reply. I have been doing a LOT of flash lately, making comforting jumps in level, but though I understand what you're doing, it does not actually make sense. For this example, the variable I want to set is a semi-global one, defined in the main timeline. Flash doesn't convert the i in the onRelease handler to a static number, which I thought it should when it's used inside a for loop.

So it writes a handler function like this, which finds when invoked, the out-serviced i variable lying around at 9

d3_mc.onRelease = function() {
    var_num = i;
    trace(i); // traces 9, which makes sense
}

It should'v been this though:

d3_mc.onRelease = function() {
    var_num = 3;
    trace(3);
}

So what you mean by scope, is that &quot;i&quot; cannot be used on the right side of the equation inside a function declaration?</description>
		<content:encoded><![CDATA[<p>Thanks for the reply. I have been doing a LOT of flash lately, making comforting jumps in level, but though I understand what you&#8217;re doing, it does not actually make sense. For this example, the variable I want to set is a semi-global one, defined in the main timeline. Flash doesn&#8217;t convert the i in the onRelease handler to a static number, which I thought it should when it&#8217;s used inside a for loop.</p>
<p>So it writes a handler function like this, which finds when invoked, the out-serviced i variable lying around at 9</p>
<p>d3_mc.onRelease = function() {<br />
    var_num = i;<br />
    trace(i); // traces 9, which makes sense<br />
}</p>
<p>It should&#8217;v been this though:</p>
<p>d3_mc.onRelease = function() {<br />
    var_num = 3;<br />
    trace(3);<br />
}</p>
<p>So what you mean by scope, is that &#8220;i&#8221; cannot be used on the right side of the equation inside a function declaration?
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-179156</link>
		<pubDate>Thu, 08 May 2008 04:11:53 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-179156</guid>
					<description>Tiemen,

I'm going through my notes, man, and I honestly can't find (i.e., can't remember) if I replied to you about the &lt;code&gt;Key&lt;/code&gt; event listener.  I hope I did!  I've been going insane lately with ridiculous schedule conflicts.

As for the &quot;lazy code&quot; question, I think it's an issue of scope.  Check out this revision:

&lt;pre&gt;&lt;code&gt;for (var i:Number = 1; i &lt; 9; i++) {
  this[&quot;d&quot; + i + &quot;_mc&quot;].var_num = i;
  this[&quot;d&quot; + i + &quot;_mc&quot;].onRelease = function() {
    trace(this.var_num);
    gotoAndStop(4);
  };
}&lt;/code&lt;/pre&gt;

I resisted the urge to add strong typing.  ;)  But this does what you want.</description>
		<content:encoded><![CDATA[<p>Tiemen,</p>
<p>I&#8217;m going through my notes, man, and I honestly can&#8217;t find (i.e., can&#8217;t remember) if I replied to you about the <code>Key</code> event listener.  I hope I did!  I&#8217;ve been going insane lately with ridiculous schedule conflicts.</p>
<p>As for the &#8220;lazy code&#8221; question, I think it&#8217;s an issue of scope.  Check out this revision:</p>
<pre><code>for (var i:Number = 1; i < 9; i++) {
  this["d" + i + "_mc"].var_num = i;
  this["d" + i + "_mc"].onRelease = function() {
    trace(this.var_num);
    gotoAndStop(4);
  };
}</code</pre>
<p>I resisted the urge to add strong typing.  <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />   But this does what you want.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Tiemen</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-172288</link>
		<pubDate>Tue, 29 Apr 2008 23:33:50 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-172288</guid>
					<description>well, trust me that the for loop is correct, for i iterates over 8.</description>
		<content:encoded><![CDATA[<p>well, trust me that the for loop is correct, for i iterates over 8.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Tiemen</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-172286</link>
		<pubDate>Tue, 29 Apr 2008 23:31:51 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-172286</guid>
					<description>Hmm here's me again with a small silly question.

Why won't this lazy code work?

&lt;pre&gt;&lt;code&gt;for (var i:Number = 1; i
  this[&quot;d&quot;+ i +&quot;_mc&quot;].onRelease = function() {
    var_num = i;
    trace(var_num);
    gotoAndStop(4);
  }
}&lt;/code&gt;&lt;/pre&gt;

Clicking on any of the 8 buttons traces &quot;9&quot;, which pretty much baffles me.</description>
		<content:encoded><![CDATA[<p>Hmm here&#8217;s me again with a small silly question.</p>
<p>Why won&#8217;t this lazy code work?</p>
<pre><code>for (var i:Number = 1; i
  this["d"+ i +"_mc"].onRelease = function() {
    var_num = i;
    trace(var_num);
    gotoAndStop(4);
  }
}</code></pre>
<p>Clicking on any of the 8 buttons traces &#8220;9&#8243;, which pretty much baffles me.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Tiemen</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-160256</link>
		<pubDate>Thu, 10 Apr 2008 10:31:40 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-160256</guid>
					<description>Next day I had this solved. I still don't understand how this works exactly, as there seem many roads that lead to Rome. The one I used in this case with the createEmptyMovieClip was:

&lt;code&gt;reference = this.createEmptyMovieClip(i +&quot;_mc&quot;, this.getNextHighestDepth());&lt;/code&gt;
&lt;code&gt;reference._x = something_crazy;&lt;/code&gt;

But this one has no (obvious) affiliation with any of the methods you mentioned! I couldn't get a &lt;code&gt;this[]&lt;/code&gt; or &lt;code&gt;eval()&lt;/code&gt; notation to work within a Key. event anyway...?!</description>
		<content:encoded><![CDATA[<p>Next day I had this solved. I still don&#8217;t understand how this works exactly, as there seem many roads that lead to Rome. The one I used in this case with the createEmptyMovieClip was:</p>
<p><code>reference = this.createEmptyMovieClip(i +"_mc", this.getNextHighestDepth());</code><br />
<code>reference._x = something_crazy;</code></p>
<p>But this one has no (obvious) affiliation with any of the methods you mentioned! I couldn&#8217;t get a <code>this[]</code> or <code>eval()</code> notation to work within a Key. event anyway&#8230;?!
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Tiemen</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-160104</link>
		<pubDate>Thu, 10 Apr 2008 01:37:48 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-160104</guid>
					<description>Incredible... I've been stuck for over two hours now on the dynamic creation of movieclips. For some reason 

&lt;code&gt;for (var i:Number = 0; i&lt;/code&gt; [code snipped by WordPress]

inside a onKeyUp event really doesn't want to work. Outside the onKeyUp, it does.
It's driving me crazy...</description>
		<content:encoded><![CDATA[<p>Incredible&#8230; I&#8217;ve been stuck for over two hours now on the dynamic creation of movieclips. For some reason </p>
<p><code>for (var i:Number = 0; i</code> [code snipped by WordPress]</p>
<p>inside a onKeyUp event really doesn&#8217;t want to work. Outside the onKeyUp, it does.<br />
It&#8217;s driving me crazy&#8230;
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-159054</link>
		<pubDate>Tue, 08 Apr 2008 00:46:38 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-159054</guid>
					<description>P.J.,

This is an issue of scope.  You're declaring a variable named &lt;code&gt;token&lt;/code&gt; inside the scope of this function, &lt;code&gt;notify()&lt;/code&gt;, yet in the condition of your &lt;code&gt;if&lt;/code&gt; statement, you're referencing &lt;code&gt;_root.token&lt;/code&gt; which, contrary to your expectations, is trying to reference a completely different &lt;code&gt;token&lt;/code&gt; variable; namely, the non-existent one scoped to the main timeline (&lt;code&gt;_root&lt;/code&gt;).  Does that make sense?  You've got two &lt;code&gt;token&lt;/code&gt; references going, one of which doesn't exist.  The one you're referencing in the condition doesn't exist.

If you want to reference &lt;code&gt;_root&lt;/code&gt; as your container object, I recommend you go with the array access operator instead:

&lt;pre&gt;&lt;code&gt;function notify() {
  for (n=1;n&lt;7;n++) {
    if (_root.jv_mc.hitTest(_root[&amp;#34;tkn&amp;#34; + n]) {
      notice_mc.gotoAndPlay(2);
    }
  }
}&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>P.J.,</p>
<p>This is an issue of scope.  You&#8217;re declaring a variable named <code>token</code> inside the scope of this function, <code>notify()</code>, yet in the condition of your <code>if</code> statement, you&#8217;re referencing <code>_root.token</code> which, contrary to your expectations, is trying to reference a completely different <code>token</code> variable; namely, the non-existent one scoped to the main timeline (<code>_root</code>).  Does that make sense?  You&#8217;ve got two <code>token</code> references going, one of which doesn&#8217;t exist.  The one you&#8217;re referencing in the condition doesn&#8217;t exist.</p>
<p>If you want to reference <code>_root</code> as your container object, I recommend you go with the array access operator instead:</p>
<pre><code>function notify() {
  for (n=1;n<7;n++) {
    if (_root.jv_mc.hitTest(_root[&quot;tkn&quot; + n]) {
      notice_mc.gotoAndPlay(2);
    }
  }
}</code></pre>
]]></content:encoded>
				</item>
	<item>
		<title>by: P.J.</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-152046</link>
		<pubDate>Sun, 23 Mar 2008 10:50:52 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-152046</guid>
					<description>I don't seem to have any luck getting variables to work. I have six identical movieclips on the stage and I would like to include them all in certain functions/statements. Right now, I had to settle for writing out an identical statement for each instance and that works fine. But it's very time consuming, especially for more complex if/else statements, and the resulting code is very unwieldy to say the least.

So if the movieclips are named tkn1 through tkn6 and I want to execute a certain code whenever any of these clips are intersecting with a movieclip named jv_mc, I tried the following and it did nothing.

&lt;pre&gt;&lt;code&gt;function notify() {
  var token:MovieClip;
  for (n=1;n&lt;7;n++) {
    token = eval(&quot;tkn&quot;+n)
    //the condition/statement
    if (_root.jv_mc.hitTest(_root.token)) {
      notice_mc.gotoAndPlay(2);
    }
  }
}&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I don&#8217;t seem to have any luck getting variables to work. I have six identical movieclips on the stage and I would like to include them all in certain functions/statements. Right now, I had to settle for writing out an identical statement for each instance and that works fine. But it&#8217;s very time consuming, especially for more complex if/else statements, and the resulting code is very unwieldy to say the least.</p>
<p>So if the movieclips are named tkn1 through tkn6 and I want to execute a certain code whenever any of these clips are intersecting with a movieclip named jv_mc, I tried the following and it did nothing.</p>
<pre><code>function notify() {
  var token:MovieClip;
  for (n=1;n<7;n++) {
    token = eval("tkn"+n)
    //the condition/statement
    if (_root.jv_mc.hitTest(_root.token)) {
      notice_mc.gotoAndPlay(2);
    }
  }
}</code></pre>
]]></content:encoded>
				</item>
	<item>
		<title>by: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-120721</link>
		<pubDate>Tue, 15 Jan 2008 02:15:34 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-120721</guid>
					<description>Billy,

Glad to help!  :)</description>
		<content:encoded><![CDATA[<p>Billy,</p>
<p>Glad to help!  <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Billy</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-120019</link>
		<pubDate>Sat, 12 Jan 2008 08:38:08 +0000</pubDate>
		<guid>http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically#comment-120019</guid>
					<description>Thanks, I was trying to group dynamically created movie clips from an XML file so that I could manipulate them as a group without just attaching them into some other movie clip. 

So I went along the lines of using an array and pushing the names into the array but couldn't figure out how to get the names in the array to affect the actual movie clips until I find this post, again thanks for the write up.</description>
		<content:encoded><![CDATA[<p>Thanks, I was trying to group dynamically created movie clips from an XML file so that I could manipulate them as a group without just attaching them into some other movie clip. </p>
<p>So I went along the lines of using an array and pushing the names into the array but couldn&#8217;t figure out how to get the names in the array to affect the actual movie clips until I find this post, again thanks for the write up.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
