Tutorial Action Script 3 - 008


In continuation with the tutorial (001 - 002 - 003 - 004 - 005 - 006 - 007)

4.3. Removing objects from the display list. removeChild ():

The process to remove the child's stage or some element is very simple and very similar to the process of the add. The method removeChild () is also present in many of the graphics, so we use only:

  removeChild (panel) 

and eliminate child's take the panel and all its contents which includes our ComboBox object.

Besides this process can also use the method removeChildAt () it receives as a parameter the number / index of (child) object in question, number / index that is automatically assigned by the display list, in the example above, the ComboBox is the child with number / index 0, because it is the only child added to the panel, to remove them to me to use:

  painel.removeChildAt (0); 

ComboBox and would be automatically removed from the panel.

Let's assume that the function on top, a textinput also added to the panel:

   TextInput(); private var text: textinput = new textinput (); 
  / / and bottom adicionaríamos to the panel: 
  painel.addChild (combo); 
  painel.addChild (text); 

Here the text will be the child with the number / index 1, and to remove that would use:

  painel.removeChild (text); 

or

  painel.removeChildAt (1); 

As you can see the process is fairly simple, but in reality, the subject was displayList only removed from, but still resident in memory, causing the garbage collector to ignore in their work, yet again whether to adopt best practices, and to actually remove these objects from memory, we must use:

  / / remove the parent / displaylist 
  painel.addChild (combo); 
  painel.addChild (text); 
  / / removing the memory: 
  Combo = null; 
  text = null; 

That is, should always remove it from memory, otherwise it will overload our application.

If on the one hand remove objects with the removeChildAt () can be fast, on the other hand may be quite painful and complicated by the simple fact that if we have 20 children's no container, we can remove the child to be wrong besides complicated know the number / the respective index, for there is something we can use to simplify the process; the property. name of the objects that identifies the object by name in displayList, in light up enough to give the names to 3 components (panel, and ComboBox textinput):

before adding them to me as the Childs, we set:

  painel.name = "meu_painel1"; 
  combo.name = "meu_combo1"; 
  texto.name = "meu_texto1"; 

and for the number of child panel, usariamos:

  ); var private object: DisplayObject = getChildByName ( "meu_painel1"); 
   = getChildIndex(objeto); var private num_painel: int = getChildIndex (object); 

and finally to remove:

  removeChildAt (num_painel); 

The same goes for Childs combo and the text:

  ); var private objeto_combo: DisplayObject = painel.getChildByName ( "meu_combo1"); 
   = getChildIndex(objeto_combo); var private num_combo: int = getChildIndex (objeto_combo); 
  painel.removeChildAt (num_combo); 
  ); var private objeto_texto: DisplayObject = painel.getChildByName ( "meu_texto1"); 
   = getChildIndex(objeto_texto); var private num_texto: int = getChildIndex (objeto_texto); 
  painel.removeChildAt (num_texto); 

Please note that we put the panel. t Toni in getChildByName as removeChild, as the child's combo and text are children of the panel.

4.4. Changing the order of Childs on display List

If for some reason, we want to change the order of Childs in the display list, sometimes we want to do it, for example by adding a button application and add them to me after a panel, the button will be covered by the panel, so to put -mos the button on the front panel simply replace them in order to display list, see:

   Button; var private btn: Button = new Button; 
   Painel; var private NLP: Panel = new Panel; 
  btn.x = 25; 
  btn.width = 125; 
  btn.y = 50; 
  btn.name = "but1"; 
  addChild (BTN); 
  pnl.x = 20; 
  pnl.y = 40; 
  pnl.width = 250; 
  pnl.height = 200; 
  pnl.name = "painel1"; 
  addChild (NLP); 

In this case we would see only the screen in our application, namely to see the button or that would add as children by another order:

  addChild (NLP); 
  addChild (BTN); 

we can see the respective positions of our components as:

  trace ( "->" + getChildAt (0). name); 
  trace ( "->" + getChildAt (1). name); 

and we will see in the output:

-> But1

-> Paine1

For the change in the order (z), we can use the following ways:

  swapChildren (btn, NLP); 

that the exchange of components in order axis zz identifying the components, or

  swapChildrenAt (0, 1); 

that the exchange, identifying by their numbers / index 's or even:

  btn.setChildIndex (2); 

that puts our button with the number / index 2, which is above the NLP (index = 1).

Here are some ways that allow change of the index components in displayList.

In the next tutorial talk more specifically about the language AS3, and its complete package of guidance to objects.

ccommons

http://creativecommons.org/licenses/by-nc-nd/3.0/deed.pt

pdf version temporarily unavailable.
Leave a comment or a Trackback

2 Trackbacks

  1. [...] 9:29 am In continuation with the tutorial (001 - 002 - 003 - 004 - 005 - 006 - 007 - 008 [...]

  2. [...] 12:55 p.m. pm In continuation with the tutorial (001 - 002 - 003 - 004 - 005 - 006 - 007 - 008 - [...]

Leave a comment

Your email is never published or shared. Required fields are marked with an *