<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.34058-</id>
  <updated>2009-11-20T00:21:06Z</updated>
  <title>Comments for The Official &quot;visible vs alpha vs removeChild()&quot; Showdown (http://www.insideria.com/2008/11/visible-false-versus-removechi.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.34058</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=34058" title="The Official &quot;visible vs alpha vs removeChild()&quot; Showdown" />
    <published>2008-11-24T14:00:00Z</published>
    <updated>2008-11-24T14:00:00Z</updated>
    <title>The Official &quot;visible vs alpha vs removeChild()&quot; Showdown</title>
    <summary><![CDATA[Flash Player&#39;s display API offers three different tools for hiding display 
objects from the screen: the visible variable, the 
alpha variable, and the removeChild() method. All 
three tools achieve the same end result&#8212;hiding a graphic&#8212;but each tool serves 
a different structural need. Hence, there is no single answer to the question 
&quot;Should I hide graphics with visible, alpha, or 
removeChild()?&quot; Instead, 
developers must choose the approach that suits the task at hand based on a 
variety of factors. Before we consider those factors, let&#39;s take a look at 

visible, alpha, and removeChild() in 
action. ]]></summary>
    <author>
      <name>Colin Moock</name>
      <uri>http://www.oreillynet.com/pub/au/568</uri>
    </author>
    
    <category term="Adobe Feed" />
    
    <category term="Features" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<P>Flash Player&#39;s display API offers three different tools for hiding display 
objects from the screen: the <CODE>visible</CODE> variable, the 
<CODE>alpha</CODE> variable, and the <CODE>removeChild()</CODE> method. All 
three tools achieve the same end result&#8212;hiding a graphic&#8212;but each tool serves 
a different structural need. Hence, there is no single answer to the question 
&quot;Should I hide graphics with <CODE>visible</CODE>, <CODE>alpha</CODE>, or 
<CODE>removeChild()</CODE>?&quot; Instead, 
developers must choose the approach that suits the task at hand based on a 
variety of factors. Before we consider those factors, let&#39;s take a look at 

<CODE>visible</CODE>, <CODE>alpha</CODE>, and <CODE>removeChild()</CODE> in 
action. </P>
<P>First, let&#39;s create two Sprite objects and assign them to two variables, 
<CODE>background</CODE> and <CODE>ball</CODE>:</P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="linecomment">// A background rectangle</span>
<span class="category1">var</span> <span class="category2">background</span>:Sprite = <span class="category1">new</span> Sprite();
<span class="category2">background</span>.graphics.<span class="category2">beginFill</span>(0x656600);
<span class="category2">background</span>.graphics.<span class="category2">lineStyle</span>(6);
<span class="category2">background</span>.graphics.drawRect(0, 0, 400, 300);

<span class="linecomment">// A circular "ball" graphic</span>
<span class="category1">var</span> ball:Sprite = <span class="category1">new</span> Sprite();
ball.graphics.<span class="category2">beginFill</span>(0xDFDA98);
ball.graphics.<span class="category2">lineStyle</span>(6);
ball.graphics.drawCircle(0, 0, 40);</pre>
</code>

</div></div> 


<P>Next, let&#39;s move <CODE>ball</CODE> over a bit:</P>


<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
ball.<span class="category2">x</span> = 275;
ball.<span class="category2">y</span> = 100;</pre>
</code>

</div></div> 

<P>Now we&#39;ll make <CODE>ball</CODE> a display child of 
<CODE>background</CODE>:</P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category2">background</span>.addChild(ball);</pre>
</code>

</div></div> 

<P>And, finally, we&#39;ll place <CODE>background</CODE> on the display list. 
Assume, for this example, that <CODE>mainCanvas</CODE> refers to a Sprite that 
is already on the display list:</P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
mainCanvas.addChild(<span class="category2">background</span>);</pre>
</code>

</div></div> 


<P>Figure 1 shows the result of the preceding code.</P>
<P>Figure 1. The <CODE>background</CODE> and <CODE>ball</CODE> objects.<BR><IMG src="http://www.insideria.com/upload/2008/11/figure1ball.jpg"></P>
<P>Now let&#39;s consider three different ways to hide <CODE>ball</CODE> from the 
screen. First, we could set <CODE>visible</CODE> to <CODE>false</CODE>, as 
follows: </P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
ball.<span class="category2">visible</span> = <span class="category1">false</span>;</pre>
</code>

</div></div> 


<P>Or, we could hide <CODE>ball</CODE> by setting <CODE>alpha</CODE> to 0, as 
follows: </P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
ball.alpha = 0;</pre>
</code>

</div></div> 

<P>Finally, we could hide <CODE>ball</CODE> by removing it from the display list 
entirely, as follows: </P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category2">background</span>.removeChild(graphic);</pre>
</code>

</div></div> 

<P>In all three cases, <CODE>ball</CODE> is hidden from view. However, in the 
first two cases (using <CODE>visible</CODE> and <CODE>alpha</CODE>), even though 

<CODE>ball</CODE> is removed from the screen, it remains a child of the 
<CODE>background</CODE> container. By contrast, in the final case (using 
<CODE>removeChild()</CODE>), <CODE>ball</CODE> is both removed from the screen 
<I>and</I> removed from the <CODE>background</CODE> container object. That 
structural distinction is an important difference to consider when choosing 
between <CODE>visible</CODE>, <CODE>alpha</CODE>, and 

<CODE>removeChild()</CODE>. </P>

<H2><strong>Choosing Between visible, alpha, and removeChild()</strong></H2>

<P>When deciding whether to use <CODE>visible</CODE>, <CODE>alpha</CODE>, or 
<CODE>removeChild()</CODE> in an application, developers should consider the 
following factors.</P>

<BLOCKQUOTE><em>In the remainder of this article the term &quot;non-visible&quot; 
  refers to an object hidden via the <CODE>visible</CODE> variable. The term 
  &quot;zero-alpha&quot; refers to an object hidden via the <CODE>alpha</CODE> 
variable.</em></BLOCKQUOTE>

<H3><strong>Factor 1: Non-visible children stay in the stacking order</strong></H3>
<P>For the first factor, let&#39;s revise our <CODE>ball</CODE> example so that 
instead of placing <CODE>ball</CODE> inside <CODE>background</CODE>, we place 
both <CODE>background</CODE> and <CODE>ball</CODE> directly inside 

<CODE>mainCanvas</CODE>. Here&#39;s the revised code: 

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="linecomment">// A background rectangle</span>
<span class="category1">var</span> <span class="category2">background</span>:Sprite = <span class="category1">new</span> Sprite();
<span class="category2">background</span>.graphics.<span class="category2">beginFill</span>(0x656600);
<span class="category2">background</span>.graphics.<span class="category2">lineStyle</span>(6);
<span class="category2">background</span>.graphics.drawRect(0, 0, 400, 300);

<span class="linecomment">// A circular "ball" graphic</span>
<span class="category1">var</span> ball:Sprite = <span class="category1">new</span> Sprite();
ball.graphics.<span class="category2">beginFill</span>(0xDFDA98);
ball.graphics.<span class="category2">lineStyle</span>(6);
ball.graphics.drawCircle(0, 0, 40);

ball.<span class="category2">x</span> = 275;
ball.<span class="category2">y</span> = 100;

<span class="linecomment">// Add background to mainCanvas, below ball</span>
mainCanvas.addChild(<span class="category2">background</span>);
<span class="linecomment">// Add ball to mainCanvas, above background</span>
mainCanvas.addChild(ball);</pre>
</code>

</div></div> 


Now let&#39;s consider the difference between <CODE>visible</CODE> and 

<CODE>removeChild()</CODE> in an example. Suppose we're making an application where the <CODE>ball</CODE> graphic from the preceding code must always appear on top of the 
<CODE>background</CODE> graphic, and the <CODE>background</CODE> must sometimes 
be hidden. Because the preceding code already adds <CODE>background</CODE> to <CODE>mainCanvas</CODE> 

before <CODE>ball</CODE>, <CODE>background</CODE> automatically appears behind 
<CODE>ball</CODE>. Hence, to "sometimes hide" <CODE>background</CODE>, we can 
simply toggle <CODE>background.visible</CODE> between <CODE>true</CODE> and 
<CODE>false</CODE>. Any time <CODE>background</CODE> reappears, it is correctly 
stacked behind <CODE>ball</CODE>. The methods required to hide and show 

<CODE>background</CODE> are as follows:
<P></P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">public</span> <span class="category1">function</span> hideBackground ():<span class="category1">void</span> {
   <span class="category2">background</span>.<span class="category2">visible</span> = <span class="category1">false</span>;
}

<span class="category1">public</span> <span class="category1">function</span> showBackground ():<span class="category1">void</span> {
   <span class="category2">background</span>.<span class="category2">visible</span> = <span class="category1">true</span>;
}</pre>
</code>

</div></div> 


<P>By contrast, if we were to use <CODE>removeChild()</CODE> to hide 
<CODE>background</CODE>, then <CODE>background</CODE> would be removed from 
<CODE>mainCanvas</CODE>&#39;s stacking order. When showing <CODE>background</CODE> 

again, we would need to carefully re-add <CODE>background</CODE> at the correct 
depth behind <CODE>ball</CODE> (using <CODE>addChildAt()</CODE>, not 
<CODE>addChild()</CODE>). The required hide and show methods would be as 
follows:</P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">public</span> <span class="category1">function</span> hideBackground ():<span class="category1">void</span> {
   <span class="linecomment">// Use an instance variable, oldBackgroundDepth, to </span>
   <span class="linecomment">// remember background's depth</span>
   oldBackgroundDepth = mainCanvas.getChildIndex(<span class="category2">background</span>);
   mainCanvas.removeChild(<span class="category2">background</span>);
}

<span class="category1">public</span> <span class="category1">function</span> showBackground ():<span class="category1">void</span> {
   mainCanvas.addChildAt(<span class="category2">background</span>, oldBackgroundDepth);
}</pre>
</code>

</div></div>

<P>In the preceding scenario, the stack-management code required in the <CODE>removeChild()</CODE> implementation makes the <CODE>removeChild()</CODE> approach more cumbersome 
to implement and maintain than the <CODE>visible</CODE> approach.</P>

<H3><strong>Factor 2: &#39;visible = false&#39; executes faster than removeChild()</strong></H3>
<P>Hiding a graphic by setting <CODE>visible</CODE> to <CODE>false</CODE> is faster than hiding it with 
<CODE>removeChild()</CODE>. The difference in speed is negligble, but could 
be a factor in a demanding application. In testing, setting a display object&#39;s 
<CODE>visible</CODE> variable proved to be approximately 43 times faster than 
calling <CODE>addChild()</CODE> or <CODE>removeChild()</CODE> on the same 
object. In Flash Player 9, on an 8-core Mac Pro running Windows Vista, 10000 

<CODE>removeChild()</CODE> calls took 300ms, whereas 10000 <CODE>visible</CODE> 
assignments took 7ms. </P>
<H3><strong>Factor 3: Non-visible and removed children have no rendering cost</strong></H3>
<P>From a rendering-performance perspective, there is no practical difference 
between the <CODE>removeChild()</CODE> and <CODE>visible=false</CODE> 
approaches. In both cases, the renderer completely skips rendering any removed 
or non-visible objects. By contrast, objects with <CODE>alpha</CODE> set to 0 do 
have a minor rendering cost. Consider the following test results, which show the 
time required to render a single frame in an application containing 1000 instances 
of a complex vector shape. In the test, Flash Player was set to 24 frames per 
second, or approximately one frame every four milliseconds. </P>


<PRE>                  Children on the                     Single-frame 
                  Display List    .visible   .alpha   Elapsed Time (ms)
No Children       0               --         --       4
Non-visible       1000            false      1        4
Zero Alpha        1000            true       0        85
Fully Visible     1000            true       1        1498
90% Transparent   1000            true       .1       1997
</PRE>

<BR>

<P>In the preceding results, notice that the time to render a frame with no 
children was exactly the same as the time to render a frame with 1000 
non-visible children. However, the time to render a frame with 1000 zero-alpha 
children was 81ms longer than the time to render a frame with 1000 non-visible children&#8212;proving that 
objects with <CODE>alpha</CODE> set to 0 have a minor rendering cost. With all 
children completely visible, the cost of rendering naturally increases, up to 
1498ms in the test. And, of course, rendering 1000 partially visible children 
took the longest, at 1997ms. In relative terms, rendering transparent, 
overlapping objects is an expensive operation.</P>
<H3><strong>Factor 4: Non-visible children affect parent dimensions</strong></H3>
<P>Suppose a container, <CODE>box</CODE>, has a single child, <CODE>icon</CODE>: 
</P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
box.addChild(icon);</pre>
</code>

</div></div> 


<P>Assuming <CODE>box</CODE> has no other content, if <CODE>icon</CODE>&#39;s width 
is 50, <CODE>box</CODE>&#39;s width will also be 50:</P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category2">trace</span>(icon.<span class="category2">width</span>);  <span class="linecomment">// 50</span>
<span class="category2">trace</span>(box.<span class="category2">width</span>);   <span class="linecomment">// 50</span></span></pre>
</code>

</div></div>


<P>And even when <CODE>icon</CODE>&#39;s <CODE>visible</CODE> variable is set to 
<CODE>false</CODE>, <CODE>box</CODE>&#39;s width, perhaps suprisingly, remains 
50:</P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
icon.<span class="category2">visible</span> = <span class="category1">false</span>;
<span class="category2">trace</span>(box.<span class="category2">width</span>);   <span class="linecomment">// Still 50! (Despite the fact that </span>
                    <span class="linecomment">// box appears empty on screen.)</span></span></pre>
</code>

</div></div>


<P>The discrepancy between <CODE>box</CODE>&#39;s on-screen appearance and its 
programmatic dimensions affects layout and collision code in the application. 
For example, when <CODE>icon</CODE> is non-visible, any layout code that wishes 
to position graphics around <CODE>box</CODE> based on <CODE>box</CODE>&#39;s 
on-screen visible size must manually ignore <CODE>icon</CODE>&#39;s dimensions:</P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="linecomment">// Place a button to the right of box.</span>
button.<span class="category2">x</span> = box.<span class="category2">x</span> + box.<span class="category2">width</span>;
<span class="category1">if</span> (icon.<span class="category2">visible</span> == <span class="category1">false</span>) {
   button.<span class="category2">x</span> -= icon.<span class="category2">width</span>;
}</pre>
</code>

</div></div>


<P>Here&#39;s a tighter way to write the preceding code:</P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="linecomment">// Place a button to the right of box.</span>
button.<span class="category2">x</span> = box.<span class="category2">x</span> + box.<span class="category2">width</span> - (!icon.<span class="category2">visible</span> ? icon.<span class="category2">width</span> : 0);</pre>
</code>

</div></div>


<P>By contrast, if <CODE>icon</CODE> is hidden via <CODE>removeChild()</CODE> 
rather than <CODE>visible</CODE>, then <CODE>box</CODE>'s 
width becomes 0, which intuitively matches its on-screen appearance.</P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
box.removeChild(icon);
<span class="category2">trace</span>(box.<span class="category2">width</span>);   <span class="linecomment">// 0</span></span></pre>
</code>

</div></div> 


<P>Once <CODE>icon</CODE> is removed, layout code can trust <CODE>box</CODE>&#39;s 
reported dimensions when positioning graphics, without any special &quot;ignore 
non-visible children&quot; conditions:</P>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="linecomment">// Place a button to the right of box.</span>
button.<span class="category2">x</span> = box.<span class="category2">x</span> + box.<span class="category2">width</span>;  <span class="linecomment">// Much nicer...</span></span></pre>
</code>

</div></div> 

<P>In a layout engine, recursivley checking containers for non-visible children 
is cumbersome and slow. I&#39;ve <A href="https://bugs.adobe.com/jira/browse/FP-741" target="_blank">filed a bug</A> requesting that 
Adobe address this issue by introducing a flag to exclude non-visible children 
in parent-bounds calculations. Please vote for the bug if you&#39;re affected by 
this issue. </P>

<H3><strong>Factor 5: Non-visible children can get in your way</strong></H3>
<P>Imagine you&#39;re creating a container, <CODE>optionsPane</CODE>, with 100 
checkboxes as children. At any given time, 10 of the checkboxes are shown on 
screen and 90 have <CODE>visible</CODE> set to <CODE>false</CODE>. To determine 
which checkboxes are checked, your program loops over <CODE>optionsPane</CODE>&#39;s 
children: </P>


<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">var</span> results:<span class="category2">Array</span> = <span class="category1">new</span> <span class="category2">Array</span>();
<span class="category1">for</span> (<span class="category1">var</span> i:<span class="category1">int</span> = 0; i &lt; optionsPane.numChildren; i++) {
   <span class="category1">if</span> (CheckBox(optionsPane.getChildAt(i)).checked) {
      results.<span class="category2">push</span>(<span class="category1">true</span>);
    } <span class="category1">else</span> {
      results.<span class="category2">push</span>(<span class="category1">false</span>);
    }
}</pre>
</code>

</div></div> 


<P>But the preceding code returns the results for all 100 checkboxes, not just 
the visible ones. To determine which <I>visible</I> checkboxes are checked, the 
loop must conditionally &quot;step over&quot; any checkbox whose <CODE>visible</CODE> 
variable is <CODE>false</CODE>: 


<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">var</span> results:<span class="category2">Array</span> = <span class="category1">new</span> <span class="category2">Array</span>();
<span class="category1">var</span> child:DisplayObject;
<span class="category1">for</span> (<span class="category1">var</span> i:<span class="category1">int</span> = 0; i &lt; optionsPane.numChildren; i++) {
   child = optionsPane.getChildAt(i));
   <span class="category1">if</span> (CheckBox(child).checked &amp;&amp; child.<span class="category2">visible</span>) {
      results.<span class="category2">push</span>(<span class="category1">true</span>);
    } <span class="category1">else</span> {
      results.<span class="category2">push</span>(<span class="category1">false</span>);
    }
}</pre>
</code>

</div></div> 

Not difficult, but it does add some noise to the loop. If the program had 
used <CODE>removeChild()</CODE> instead of <CODE>visible</CODE> to hide the 
checkboxes, the loop wouldn&#39;t need to &quot;step over&quot; non-visible checkboxes. 

<P></P>
<H3><strong>Factor 6: Objects with alpha set to 0 receive mouse events</strong></H3>
<P>Unlike objects with <CODE>visible</CODE> set to <CODE>false</CODE>, objects 
with <CODE>alpha</CODE> set to 0 are considered interactive, and can receive 
mouse events. For example, if you place your mouse pointer over a zero-alpha 
object, Flash Player dispatches a MouseEvent.MOUSE_OVER event targeted at that 
object. Zero-alpha objects, hence, can be useful for creating non-visual 
elements that capture user input, such as &quot;invisible buttons&quot; or &quot;easter eggs.&quot; 

</P>
<H2><strong>General Guidelines to Follow</strong></H2>
<P>With the preceding factors in mind, let&#39;s look at a few guidelines to follow 
when deciding between <CODE>visible</CODE>, <CODE>alpha</CODE>, and 
<CODE>removeChild()</CODE>. The following strategies are particularly useful in 
visual applications that repeatedly toggle graphics between visible and hidden 
states. </P>
<H3><strong>Recommendation 1: Use removeChild() for predictability</strong></H3>

<P>When in doubt, use <CODE>removeChild()</CODE> instead of <CODE>visible</CODE> 
because using <CODE>visible</CODE> can produce misleading parent dimensions. See 
Factor 4. Particularly avoid <CODE>visible</CODE> when visual dimensions matter, 
such as when testing for collisions in a game or when arranging graphics 
programmatically. Note that this recommendation would be made obsolete if Adobe 
were to address <A href="https://bugs.adobe.com/jira/browse/FP-741" target="_blank">Flash Player 
Bug 741</A>. </P>
<H3><strong>Recommendation 2: Use visible for performance and convenient depth 
management</strong></H3>

<P>When speed and stacking order are your key concerns, use <CODE>visible</CODE> 
instead of <CODE>removeChild()</CODE> because setting <CODE>visible</CODE> is 
faster than calling <CODE>removeChild()</CODE> (see Factor 2), and using 
<CODE>visible</CODE> facilitates easy depth management (see Factor 1). But be 
careful when working with dimensions in layout and collision code (see Factor 
4). Furthermore, remember that even in applications that use 
<CODE>visible</CODE> to hide graphics, when a graphic is no longer needed, it 
should be removed from its parent container via <CODE>removeChild()</CODE>, and 
then dereferenced so it can be garbage collected. See the section &quot;<em>Not visible</em> 

does not mean <em>deleted</em>&quot;, below. </P>
<H3><strong>Recommendation 3: Generally avoid alpha</strong></H3>
<P>Avoid using <CODE>alpha</CODE> to completely hide graphics because zero-alpha 
graphics take longer to render than both non-visible graphics and removed 
graphics. See Factor 3 and Factor 6. Hide graphics with <CODE>alpha</CODE> only 
when creating interactive hidden-graphics.</P>
<H2><strong>Related Considerations</strong></H2>
<P>When removing graphical assets from the screen, bear in mind the following 
issues, which are important regardless of whether you&#39;re using 

<CODE>visible</CODE>, <CODE>alpha</CODE>, or <CODE>removeChild()</CODE>. </P>
<H3><strong><em>Not visible</em> does not mean <em>deleted</em></strong></H3>
<P>Setting an object&#39;s <CODE>visible</CODE> to <CODE>false</CODE> or 

<CODE>alpha</CODE> to 0 does <I>not</I> remove that object from memory. 
Likewise, removing an object from the display list via 
<CODE>removeChild()</CODE> does not remove that object from memory. Display 
objects are removed from memory only once they have been <A href="http://www.moock.org/blog/archives/000279.html" target="_blank">deactivated, dereferenced, 
and garbage collected</A>. Regardless of your application&#39;s visibility strategy, 
be sure to deactivate and dereference your display objects when they are no 
longer needed. </P>
<H3><strong>Off-screen MovieClip timelines can be hazardous</strong></H3>
<P>Suppose you have an on-screen MovieClip object whose timeline is playing. If 
you hide that MovieClip using <CODE>visible</CODE>, <CODE>alpha</CODE>, or 

<CODE>removeChild()</CODE>, its timeline will continue to play. As the timeline plays, it will have several negative side-effects: </P>
<UL>
  <LI>The timeline playback will consume system resources. 
  <LI>If the playhead advances to a 
  frame with code, that code will execute, potentially wasting system resources 
  and triggering undesirable program side-effects. 
  <LI>If the MovieClip object&#39;s visual contents change when a new frame is reached, the 
  object&#39;s dimensions will also change, potentially affecting parent dimensions (see 
  Factor 4). </LI></UL>

<P>Therefore, as a general rule, remember to stop the playback of all MovieClip 
objects that are not on screen. </P>
<P>Thus ends our examination of visibility management in ActionScript. Hopefully 
it has helped you navigate some of ActionScript&#39;s subtler display-programming 
issues. My thanks to Jim Corbett, lead engineer on Flash Player&#39;s display API, 
who fielded my research questions during the writing of this article.</P>

<P>Happy coding!</P>]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2046858</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2046858" />
    <title>Comment from Richard Davey on 2008-11-24</title>
    <author>
        <name>Richard Davey</name>
        <uri>http://www.photonstorm.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.photonstorm.com">
        <![CDATA[<p>Very interesting and useful. Do you know if the time taken to render zero alpha objects exists because of the events they can still receive? I.e. would 1000 mouse disabled objects still consume as much render time?</p>]]>
    </content>
    <published>2008-11-24T14:46:36Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2046863</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2046863" />
    <title>Comment from wic on 2008-11-24</title>
    <author>
        <name>wic</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Another approach, and a favourite of mine, is the following:</p>

<p>   &lt;mx:Panel&gt;<br />
	    &lt;mx:Button label="b1"<br />
			id="b"<br />
			visible="{someCondition}" <br />
 		  	includeInLayout="{b1.visible}"/&gt;</p>

<p>	    &lt;mx:Button label="b2"/&gt;</p>

<p>   &lt;/mx:Panel&gt;<br />
...</p>

<p>This makes the panel resize itself and move b2 according with no need to manually calculate anything (as  you do in factor 4).</p>]]>
    </content>
    <published>2008-11-24T15:26:43Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2046905</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2046905" />
    <title>Comment from Rick Winscot on 2008-11-24</title>
    <author>
        <name>Rick Winscot</name>
        <uri>http://www.quilix.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.quilix.com">
        <![CDATA[<p>Did you experiment with includeInLayout at all? It would be interesting to see how this property plays in your stats.</p>]]>
    </content>
    <published>2008-11-24T21:43:51Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2046911</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2046911" />
    <title>Comment from colin on 2008-11-24</title>
    <author>
        <name>colin</name>
        <uri>http://moock.org</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://moock.org">
        <![CDATA[<p>hi rick,<br />
no, in an effort to keep the topic focused, i didn't do any flex framework-specific testing. feel free to post results if you do any.</p>

<p>colin</p>]]>
    </content>
    <published>2008-11-24T22:05:04Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2046973</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2046973" />
    <title>Comment from Fardeen on 2008-11-25</title>
    <author>
        <name>Fardeen</name>
        <uri>http://fardeen.biz</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://fardeen.biz">
        <![CDATA[<p>Very interesting ! Thk you for sharing those great infos.</p>]]>
    </content>
    <published>2008-11-25T14:27:44Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2046975</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2046975" />
    <title>Comment from Jason Langdon on 2008-11-25</title>
    <author>
        <name>Jason Langdon</name>
        <uri>http://blog.wrench.com.au</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://blog.wrench.com.au">
        <![CDATA[<p>I knew there was a reason why I used to insert a blank keyframe with a stop() action at the end of my tweens when they alpha'd to 0 ;-)</p>]]>
    </content>
    <published>2008-11-25T14:41:58Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2047085</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2047085" />
    <title>Comment from Tink on 2008-11-26</title>
    <author>
        <name>Tink</name>
        <uri>http://www.tink.ws/blog</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.tink.ws/blog">
        <![CDATA[<p>'Flash Player's display API offers three different tools for hiding display objects'</p>

<p>Actually 4. I use BlendMode.ERASE in Efflex.</p>]]>
    </content>
    <published>2008-11-26T08:45:25Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2047431</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2047431" />
    <title>Comment from david doull on 2008-11-26</title>
    <author>
        <name>david doull</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>another option - set the sprites x value to a value that is off the stage x=-100<br />
Not sure about flash 10 but in earlier versions this gave faster rendering than visible =false;</p>]]>
    </content>
    <published>2008-11-26T16:39:03Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2047992</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2047992" />
    <title>Comment from Chris McAndrew on 2008-12-02</title>
    <author>
        <name>Chris McAndrew</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Thanks Colin, <br />
This is one of those topics most people think is a no-brainer, when in reality, it can and does affect performance and depth depending on how it's used. As always, thanks for your research. </p>]]>
    </content>
    <published>2008-12-03T04:54:47Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2054736</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2054736" />
    <title>Comment from Philip Bulley on 2009-03-09</title>
    <author>
        <name>Philip Bulley</name>
        <uri>http://www.milkisevil.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.milkisevil.com">
        <![CDATA[<p>Further to Tink's comment, I always use BlendMode.ERASE in place of alpha=0, as any MouseEvent can still be captured. Haven't benchmarked it myself, but have been assured it is faster than alpha. Would be interesting to see this method added to your tests.</p>]]>
    </content>
    <published>2009-03-09T10:01:44Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2054737</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2054737" />
    <title>Comment from Philip on 2009-03-09</title>
    <author>
        <name>Philip</name>
        <uri>http://www.milkisevil.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.milkisevil.com">
        <![CDATA[<p>Further to Tink's comment, I always use BlendMode.ERASE in place of alpha=0, as any MouseEvent can still be captured. Haven't benchmarked it myself, but have been assured it is faster than alpha. Would be interesting to see this method added to your tests.</p>]]>
    </content>
    <published>2009-03-09T10:06:46Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2074278</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2074278" />
    <title>Comment from facildelembrar on 2009-08-29</title>
    <author>
        <name>facildelembrar</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Non-visible objects affect parent dimensions, so they also affect the hitTestPoint function and possibly the hitTestObject too. This is probably the same for masked objects.</p>

<p>I just spend several hours trying to fix a bug related to non-visible objects, when the best solution was to simply remove them...</p>

<p>Colin, thank you so much for this article.<br />
Adobe, please fix this bug 741 or add some visibleWidth/visibleHeight properties for crying out loud.</p>]]>
    </content>
    <published>2009-08-29T22:22:00Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2091112</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2091112" />
    <title>Comment from Guillaume Provost on 2009-09-09</title>
    <author>
        <name>Guillaume Provost</name>
        <uri>http://www.compulsiongames.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.compulsiongames.com">
        <![CDATA[<p>Hey guys,</p>

<p>   I had the interesting problem of dealing with (external/legacy) interactive invisible elements that were seriously affecting performance on my codebase with "alpha=0". I've found the following workaround to the problem for Flash 10 apps to keep interactivity working and performance high:</p>

<p>   On Event.ENTER_FRAME set visible=true<br />
   On Event.EXIT_FRAME call stage.invalidate()<br />
   On Event.RENDER call visible=false</p>

<p>   This will effectively save the performance cost associated with rendering the primitives, while keeping them active in the interactive event flow.</p>]]>
    </content>
    <published>2009-09-10T05:31:29Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2186561</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2186561" />
    <title>Comment from vito on 2009-11-14</title>
    <author>
        <name>vito</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>great info, thanks</p>]]>
    </content>
    <published>2009-11-14T18:35:57Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34058-comment:2193629</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34058" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/visible-false-versus-removechi.html#comment-2193629" />
    <title>Comment from Simon Hartigan on 2009-11-19</title>
    <author>
        <name>Simon Hartigan</name>
        <uri>http://www.trafikk.us</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.trafikk.us">
        <![CDATA[<p>Thank you very much for this!  I actually just figured it out by my own (then I searched for an article to explain it) that visible is my preferred choice over removeChild(), however you helped explain it nicely and I appreciate that!</p>]]>
    </content>
    <published>2009-11-20T00:21:00Z</published>
  </entry>

</feed
