Tutorial 5 – Nodes

Nodes further refered as GNodes by their class name are abstract building blocks of Genome2D’s render graph, they decide the position and hierarchy not the actual functionality whis is added later using components.

GNodes can be compared to standard Flash display list functionality (or other popular 2D frameworks) as their imitate the same hierarchy and positioning information or the whole scene. The hierarchy starts a root node that is automatically created by Genome2D upon initialization and is always the top most node of the render graph. Any node when added to render graph can have exactly one parent and unlimited number of children, once a node is added to another parent it is automatically removed from its previous parent. It basically means that a node can be at a single time only at a single place in the tree hierarchy.

Additionally by default Genome2D uses the same screen space coordinate system as Flash so 0,0 is at the top left of the screen X increases to the right and Y increases to bottom of the screen. This behavior is also extendable and overridable using custom cameras. Nodes in the list are also rendered from first to last using painters algorithm same as Flash native display list.

To create GNodes as everything in Genome2D you will need to use factory methods designed to do so using. The basic method to create an empty GNode is

// The name argument is optional
GNode.createNode("nodeName");

There are additional factory nodes that can be used and will be covered in later tutorials such as:

GNodeFactory.createNodeWithComponent(componentClass, nodeName, componentLookupClass);
GNode.createNodeFromPrototype(prototypeXML, nodeName);

First node you will create will always need to be added to Genome2D’s root and to achieve that and manipulate with nodes inside the render graph there are set of familiar and some not some familiar methods. Some of there were also extended for additional functionality.

// Add child to a node, before argument is optional and if specified the child will not be added as the last child but inserted before a specified child instead
parentInstance.addChild(childInstance, before);

// WARNING since Genome2D uses linked lists for performance indexing is taxing so use with care.
// Add child to a node at a specified index
parentInstance.addChildAt(childInstance, index);

// WARNING since Genome2D uses linked lists for performance indexing is taxing so use with care.
// Get a child at a specified index
parentInstance.getChildAt(index);

// WARNING since Genome2D uses linked lists for performance indexing is taxing so use with care.
// Get an index of a child
parentInstance.getChildIndex(childInstance);

// WARNING since Genome2D uses linked lists for performance indexing is taxing so use with care.
// Set an index of a child this will move the child in the list
parentInstance.setChildIndex(childInstance, index);

// WARNING since Genome2D uses linked lists for performance indexing is taxing so use with care.
// Swap the position of children at specified indices
parentInstance.swapChildrenAt(index1, index2);

// Swap the position of specified children
parentInstance.swapChildren(childInstance1, childInstance2);

// This will put the specified child to the end of the list so it will be the last rendered
parentInstance.putChildToFront(childInstance);

// This will put the specified child to the beginning of the list so it will be the first rendered
parentInstance.putChildToBack(childInstance);

// Remove child from a node
parentInstance.removeChild(childInstance);

// WARNING since Genome2D uses linked lists for performance indexing is taxing so use with care.
// Remove child at a specified index
parentInstance.removeChildAt(childInstance, index);

// This method will remove and dispose all children nodes
parentInstance.disposeChildren();

As you can see in some of the previous methods I am mentioning that Genome2D uses linked lists to store children references, this is purely for performance reasons in most use cases however it also impacts any kind of indexing which also means most of the well known child manipulation methods. I added these methods anyway since many people are used to them and if used correctly their impact isn’t an issue but you should definitely avoid them if there is an option for different implementation.

This also affects iteration as using the standard approach of indexed iteration would be a performance killer, so instead use linked list iterative method to iterate children in Genome2D.

for (var child:GNode = parent.firstChild; child; child = child.nextNode) {
   // Do something with child
}