Creating a small GUI engine: visible versus addChild / removeChild

I am currently experimenting with a very simple graphical interface ... "engine" (I think you could call it). Its essence:

  • There is a FrontController that falls under user requests; every request has a uid
  • each uid (read "page") has an announcement about the components ("modules") that are present on it
  • are subclasses of Sprite and, in fact, are unique

Naturally, I need a way to hide / show these sprites. For the time being, it seems to me pretty much what Flex has by default - in the sense that "if we are in the place where comp is displayed, create it, cache it and reuse it every time it is displayed again "

The question is what would be a more appropriate and effective way of hiding and displaying - through addChild/ removeChildor switching visible.

As I see it:

  • visible is fast and dirty (during the first tests)
  • visibledoes not create a chain of bubble events like Event.ADDEDorEvent.REMOVED
  • Invisible components do not receive mouse events.

So, it removeChildwill be what I would call when I'm sure that the component will no longer be needed on the screen (or the cache is too big, for example)

stackoverflow'ers/AS3-crazed?

: ( google).

i visible; , -, ; " FLASH PLATFORM" Adobe . 69 .

, , :

package 
{
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.getTimer;

/**
 * Simple benchmark to test alternatives for hiding and showing
 * DisplayObject.
 * 
 * Use:
 * <code>
 * new DisplayBM(stage);
 * </code>
 * 
 * Hit:
 * - "1" to addChild (note that hitting it 2 times is expensive; i think
 * this is because the player has to check whether or not the comp is
 * used elsewhere)
 * - "q" to removeChild (2 times in a row will throw an exception) 
 * - "2" to set visible to true
 * - "w" to set visible to false
 * 
 * @author Vasi Grigorash
 */    
public class DisplayBM{
    public function DisplayBM(stage:Stage){
        super();

        var insts:uint = 5000;
        var v:Vector.<Sprite> = new Vector.<Sprite>(insts);
        var i:Number = v.length, s:Sprite
        while (i--){
            s = new Sprite;
            s.graphics.beginFill(Math.random() * 0xFFFFFF);
            s.graphics.drawRect(
                Math.random() * stage.stageWidth, 
                Math.random() * stage.stageHeight,
                10, 
                10
            );
            s.graphics.endFill();
            v[i] = s;
        }

        var store:Object = {};
        store[Event.ADDED] = null;
        store[Event.REMOVED] = null;
        var count:Function = function(e:Event):void{
            store[e.type]++;
        }
        var keydown:Function = function (e:KeyboardEvent):void{
            var key:String
            //clear event counts from last run
            for (key in store){
                store[key] = 0;
            }

            stage.addEventListener(Event.ADDED, count);
            stage.addEventListener(Event.REMOVED, count);

            var s0:uint = getTimer(), op:String;
            var i:Number = v.length;
            if (e.keyCode === Keyboard.NUMBER_1){
                op = 'addChild';
                while (i--){
                    stage.addChild(v[i]);
                }
            }
            if (e.keyCode === Keyboard.Q){
                op = 'removeChild';
                while (i--){
                    stage.removeChild(v[i]);
                }
            }
            if (e.keyCode === Keyboard.NUMBER_2){
                op = 'visibile';
                while (i--){
                    v[i].visible = true;
                }
            }
            if (e.keyCode === Keyboard.W){
                op = 'invisibile';
                while (i--){
                    v[i].visible = false;
                }
            }
            if (op){
                //format events
                var events:Array = [];
                for (key in store){
                    events.push(key + ' : ' + store[key])
                }

                trace(op + ' took ' + (getTimer() - s0) + ' ' + events.join(','));
            }

            stage.removeEventListener(Event.ADDED, count);
            stage.removeEventListener(Event.REMOVED, count);
        }

        //autodispatch
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
    }
}
}
+5
3

( ) /.

, addChild , .

EDIT: Adobe http://help.adobe.com/en_US/as3/mobile/WS5d37564e2b3bb78e5247b9e212ea639b4d7-8000.html, , GPU = ( ). :

, . Overdrawing , . , . , , , . , . , , .

, , . , GPU. , .

+2

, -. , . , , , ,

- , . , , , , . - , , , , , lol

, , . "" im memory .

+1

Here are some solid data on this from Moock: http://www.developria.com/2008/11/visible-false-versus-removechi.html

                  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
0
source

All Articles