addChild() and removeChild() are methods which add and remove visual elements on the stage.
addChild(displayObject);- "displayObject" is the object that must be displayed in the Flash presentation (such as: a text field, a geometric figure, an image, etc.).
var texts:Array = ['courses', 'tutorials', 'coursesweb.net']; // Array with texts to display var bcolor:Array = [0xe8e9fe, 0xeded01, 0xfedada]; // Array with colors for the background of each text var lung:int = 110; // Variable used for the lenght of the text field // Read through the "texts" Array for(var i:int=0; i<texts.length; i++) { // creates a field "TextField" for each element in texts // Sets the fields height, lenght, and distance "x" depending on "i" (for liniar display) // Adds background color from "bcolor" Array and text from the current element in "texts" var txt:TextField = new TextField(); txt.height = 25; txt.width = lung; txt.x = i*(lung + 8); txt.backgroundColor = bcolor[i]; txt.background = true; txt.text = texts[i]; addChild(txt); // Add the field in the scene. }- The code "for(var i:int=0; i<texts.length; i++)" reads the "texts" Array and for each current element it creates a Text Field object, and "addChild(txt);" adds this "txt" object on the stage , with all it's settings (size, background color, text, etc.).
removeChild(removeObject);- "removeObject" is the object that must be deleted from the flash presentation (such as: a text field, a geometric figure, an image).
var texts:Array = ['courses', 'tutorials', 'marplo.net']; // Array with text to display. var bcolor:Array = [0xe8e9fe, 0xeded01, 0xfedada]; // Array with colors for the background of each text var lung:int = 100; // Variable used for the lenght of the text field // Reads through the "texts" Array for(var i:int=0; i<texts.length; i++) { // creates a field "TextField" for each element in texts // Sets the fields height, lenght, and distance "x" depending on "i" (for liniar display) // Adds background color from "bcolor" Array and text from the current element in "texts" var txt:TextField = new TextField(); txt.height = 25; txt.width = lung; txt.x = i*(lung + 8); txt.backgroundColor = bcolor[i]; txt.background = true; txt.text = texts[i]; addChild(txt); // Adds the field on the stage } // Recording CLICK event for the button on the stage, "remove_bt" remove_bt.addEventListener(MouseEvent.CLICK, removeIns); // The function called by the registered event function removeIns(evt:MouseEvent):void { removeChild(txt); // Delete's the element with the instance "txt" }- This script adds three text fields (as in the previous example) in the flash presentation, above the button from the stage. When the button is pressed, this activates the "MouseEvent.CLICK" event that calls the "removeIns()" function.
When elements are added with "addChild", they are retained in memory, in an Array in the adding order, starting the indexing from the number of those added on the stage. For example, if on the stage there's only a button , it has the index 0 and the following are indexed as numbered after it (1, 2, ...).
The "removeChildAt()" method can delete elements in a Flash presentation, according to their index:
removeChildAt(index)- When an element is deleted, the index order in the Array from memory is updated, so, the element that is recorded after the deleted one will take it's index, the following one decreases with a unit, and so on. You can understand it better from the following example witch is a new version of the previous one.
var texts:Array = ['courses', 'tutorials', 'marplo.net']; // Array with text to display var bcolor:Array = [0xe8e9fe, 0xeded01, 0xfedada]; // Array with colors for the background of each text var lung:int = 100; // Variable used for the lenght of the text field // Reads through the "texts" Array for(var i:int=0; i<texts.length; i++) { // creates a field "TextField" for each element in texts // Sets the fields height, lenght, and distance "x" depending on "i" (for liniar display) // Adds background color from "bcolor" Array and text from the current element in "texts" var txt:TextField = new TextField(); txt.height = 25; txt.width = lung; txt.x = i*(lung + 8); txt.backgroundColor = bcolor[i]; txt.background = true; txt.text = texts[i]; addChild(txt); // Adds the field on the stage } // Recording CLICK event for the button on the stage, "remove_bt" remove_bt.addEventListener(MouseEvent.CLICK, removeIns); // The function called by the registered event function removeIns(evt:MouseEvent):void { removeChildAt(2); // Deletes the element with index 2 }- When the button is pressed, the comand "removeChildAt(2);" deletes the element with index 2. Two text fields remain on the stage (the first and the third, but the memory index gets updated, so, the field that had the index 3 will get index 2, fact demonstrated if the deletion button is pressed again. According to the code, it will always delete the element with the index 2. When on the stage the are no more elements that can take the place of the one deleted, pressing the button will give an error.
Besides the numerical index of the elements, they are also recorded with an unique name, that will not change when an element is deleted.
Each element added in the flash presentation has a unique name. If the name of the element is not specified, will be automaticaly added by the program with the name "instanceNR" ('NR' is the serial number: "instance12", "instance13", ...).
This name can be defined with the property name, with the following syntax:
insObj.name- "insObj" is the instance of the element added in the Flash presentation.
getChildByName("name")- "name" is the name defined with "insObj.name".
var texts:Array = ['courses', 'tutorials', 'marplo.net']; // Array with text to display var bcolor:Array = [0xe8e9fe, 0xeded01, 0xfedada]; // Array with colors for the background of each text var lung:int = 100; // Variable used for the lenght of the text field // Reads through the "texts" Array for(var i:int=0; i<texts.length; i++) { // creates a field "TextField" for each element in texts // Sets the fields height, lenght, and distance "x" depending on "i" (for liniar display) // Adds background color from "bcolor" Array and text from the current element in "texts" var txt:TextField = new TextField(); txt.height = 25; txt.width = lung; txt.x = i*(lung + 8); txt.backgroundColor = bcolor[i]; txt.background = true; txt.text = texts[i]; // Atributin a name to the instance, for it to be unique also use 'i's value txt.name = 'camp'+i; addChild(txt); // Adds the field on the stage } // Recording CLICK event for the button on the stage, "remove_bt" remove_bt.addEventListener(MouseEvent.CLICK, removeIns); // The function called by the registered event function removeIns(evt:MouseEvent):void { removeChild(getChildByName('camp0')); // Deletes the element with 'camp0' name removeChild(getChildByName('camp1')); // Deletes the element with 'camp1' name }- the "txt.name = 'camp'+i;" expression within the "for()" loop gives each "txt" instance a registration name: camp0, camp1, camp2.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net