<?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: ADDED_TO_STAGE event fires twice</title>
	<atom:link href="http://www.orlandmedia.com/blog/actionscript-3/added_to_stage-event-fires-twice/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.orlandmedia.com/blog/tutorials/added_to_stage-event-fires-twice/</link>
	<description>Web Consultancy, Development, Training &#38; Hosting</description>
	<lastBuildDate>Tue, 26 Jul 2011 17:33:01 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Orland Media</title>
		<link>http://www.orlandmedia.com/blog/tutorials/added_to_stage-event-fires-twice/comment-page-1/#comment-253</link>
		<dc:creator>Orland Media</dc:creator>
		<pubDate>Fri, 15 Jul 2011 09:40:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.orlandmedia.com/?p=1164#comment-253</guid>
		<description>&lt;p&gt;&lt;a href=&quot;http://bugs.adobe.com/jira/browse/FP-1569&quot; rel=&quot;nofollow&quot;&gt;Adobe report&lt;/a&gt; that this bug has been fixed in the latest Flash Player release, but on our testing with player 10.3.181.34 in Firefox 5, it still occurs. We haven&#039;t tried in Beta 11. How about you?&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p><a href="http://bugs.adobe.com/jira/browse/FP-1569" rel="nofollow">Adobe report</a> that this bug has been fixed in the latest Flash Player release, but on our testing with player 10.3.181.34 in Firefox 5, it still occurs. We haven&#8217;t tried in Beta 11. How about you?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Orland Media</title>
		<link>http://www.orlandmedia.com/blog/tutorials/added_to_stage-event-fires-twice/comment-page-1/#comment-45</link>
		<dc:creator>Orland Media</dc:creator>
		<pubDate>Wed, 22 Apr 2009 12:08:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.orlandmedia.com/?p=1164#comment-45</guid>
		<description>Hi Lee,

Thanks for the comment.

Yes, that solution does work fine (it&#039;s in &lt;a href=&quot;http://bugs.adobe.com/jira/browse/FP-1569&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;Adobe&#039;s bug report&lt;/a&gt;), providing you do not remove the item from the Display List then add it again - because then there&#039;s no &lt;code&gt;init&lt;/code&gt; handler.

We&#039;ve had a further look at the issue and put together a few files for testing. We called your fix &quot;Fix 1&quot; and the other one &quot;Fix 2&quot;. See what you think:

[download id=&quot;1&quot; format=&quot;1&quot;]

Your fix can be made to work fully by us adding the init handler again on removal, and that&#039;s what we&#039;ve done in the files. So it looks like this:

[as3 entities=&quot;1&quot;]
public function SquareInner()
{
  addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
  addEventListener(Event.REMOVED_FROM_STAGE, onRemove, false, 0, true);
}

/***************************************************
Instance methods
***************************************************/
 
private function init(e:Event):void
{
  // remove handler so it only fires once:
  removeEventListener(Event.ADDED_TO_STAGE, init, false);
  trace(e.target + &quot; was added to the stage&quot;);
}
 
private function onRemove(e:Event):void
{
  trace(this + &quot; was removed from stage - adding init handler again&quot;);
  addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
[/as3]

This method might actually be neater than Fix 2 (it is less verbose).

We have to be so careful not to leave stray event handlers in AS3 because they can lead to memory leaks. Both of these methods leave the ADDED_TO_STAGE handler in the object while it is off the Display List, but this should not be a problem for GC as it it not repeatedly firing and doesn&#039;t depend on the stage.

This is quite a fiddly issue. But inside the &quot;Fix 1&quot; folder, there&#039;s another folder called &quot;With Inheritance&quot;. Here we think we have really cracked it, by taking care of business in a superclass. You then override its methods with further code. Let us know what you think about it all anyway and if you spot any problems with this approach.</description>
		<content:encoded><![CDATA[<p>Hi Lee,</p>
<p>Thanks for the comment.</p>
<p>Yes, that solution does work fine (it&#8217;s in <a href="http://bugs.adobe.com/jira/browse/FP-1569" target="_blank" rel="nofollow">Adobe&#8217;s bug report</a>), providing you do not remove the item from the Display List then add it again &#8211; because then there&#8217;s no <code>init</code> handler.</p>
<p>We&#8217;ve had a further look at the issue and put together a few files for testing. We called your fix &#8220;Fix 1&#8243; and the other one &#8220;Fix 2&#8243;. See what you think:</p>
<p>[download id="1" format="1"]</p>
<p>Your fix can be made to work fully by us adding the init handler again on removal, and that&#8217;s what we&#8217;ve done in the files. So it looks like this:</p>
<pre class="brush: as3; title: ; notranslate">
public function SquareInner()
{
  addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
  addEventListener(Event.REMOVED_FROM_STAGE, onRemove, false, 0, true);
}

/***************************************************
Instance methods
***************************************************/

private function init(e:Event):void
{
  // remove handler so it only fires once:
  removeEventListener(Event.ADDED_TO_STAGE, init, false);
  trace(e.target + &quot; was added to the stage&quot;);
}

private function onRemove(e:Event):void
{
  trace(this + &quot; was removed from stage - adding init handler again&quot;);
  addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
</pre>
<p>This method might actually be neater than Fix 2 (it is less verbose).</p>
<p>We have to be so careful not to leave stray event handlers in AS3 because they can lead to memory leaks. Both of these methods leave the ADDED_TO_STAGE handler in the object while it is off the Display List, but this should not be a problem for GC as it it not repeatedly firing and doesn&#8217;t depend on the stage.</p>
<p>This is quite a fiddly issue. But inside the &#8220;Fix 1&#8243; folder, there&#8217;s another folder called &#8220;With Inheritance&#8221;. Here we think we have really cracked it, by taking care of business in a superclass. You then override its methods with further code. Let us know what you think about it all anyway and if you spot any problems with this approach.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lee</title>
		<link>http://www.orlandmedia.com/blog/tutorials/added_to_stage-event-fires-twice/comment-page-1/#comment-43</link>
		<dc:creator>Lee</dc:creator>
		<pubDate>Tue, 21 Apr 2009 21:07:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.orlandmedia.com/?p=1164#comment-43</guid>
		<description>Solved?
[as3]
public function init(e:Event):void
{
  // remove the listener so init will only be called once....
  removeEventListener(Event.ADDED_TO_STAGE, init);
  // code dependent on stage property
  y = stage.stageHeight-100;
}
[/as3]</description>
		<content:encoded><![CDATA[<p>Solved?</p>
<pre class="brush: as3; title: ; notranslate">
public function init(e:Event):void
{
  // remove the listener so init will only be called once....
  removeEventListener(Event.ADDED_TO_STAGE, init);
  // code dependent on stage property
  y = stage.stageHeight-100;
}
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

