<?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/actionscript-3/added_to_stage-event-fires-twice/</link>
	<description>The blog of Orland Media Ltd</description>
	<lastBuildDate>Mon, 23 Aug 2010 22:36:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Gavin</title>
		<link>http://www.orlandmedia.com/blog/actionscript-3/added_to_stage-event-fires-twice/comment-page-1/#comment-18</link>
		<dc:creator>Gavin</dc:creator>
		<pubDate>Wed, 22 Apr 2009 13:08:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.orlandmedia.com/blog/?p=146#comment-18</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; 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.

I&#039;ve had a further look at the issue and put together a few files for testing. I 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 I&#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 I think I have really cracked it, by taking care of business in a superclass. You then override its methods with further code. Let me 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" 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>I&#8217;ve had a further look at the issue and put together a few files for testing. I 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 I&#8217;ve done in the files. So it looks like this:</p>
<pre class="brush: as3;">
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 I think I have really cracked it, by taking care of business in a superclass. You then override its methods with further code. Let me 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/actionscript-3/added_to_stage-event-fires-twice/comment-page-1/#comment-17</link>
		<dc:creator>Lee</dc:creator>
		<pubDate>Tue, 21 Apr 2009 21:07:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.orlandmedia.com/blog/?p=146#comment-17</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;">
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>
