# CocosNode Functionality

The fact that all objects depicting the scene are CocosNodes subclasses means that they share a common core of characteristics and functionalities. You know for one and you know for all.

The common core can be broken by functionality in

# Parent-child

> - .add(self, child, z=0, name=None ): Adds a child, raises exception Name already exists if duplicated
> - .remove(self, name_or_obj): Removes a child given its name or object, raises exception Child not found if not present
> - .kill(self): Remove itself from its parent
> - .parent: property giving the parent node or None
> - .get_ancestor(self, klass): Returns the first ancestor that isinstance of klass, or None
> - .get_children(self): Returns a list with the node children, order is back to front (ascending z)
> - .get(self, name): Gets a child given its name, raises exception Child not found if not present
> - operator in , as 'in node1 in node2': returns True if and only if node1 is child of node 2

Parent-Child examples

# Spatial Placement

A node position is defined relative to the parent node, most precisely with respect the parent position.

> - .x, .y .position: properties giving position relative to parent origin; updating x or y automatically updates position, the reverse is also true.
> - .anchor_x, .anchor_y, anchor: properties giving the center for the scale and rotation, they automatically maintain the relation anchor == (anchor_x, anchor_y). Notice that different CocosNodes subclasses can have different default values for anchor.
> - .scale : 1.0 is the default scale, with 2.0 the node will double its apparent size.
> - .rotation : in degrees

Spatial Placement snippets; also most of scripts in directory tests do explicit placement.

# Going in or out of the active scene

> - .on_enter(self): called if added to a parent while parent in the active scene or if the entire scene goes active
> - .on_exit(self): called if removed from parent while parent in the active scene or if the entire scene

# Render

> - .visit(self): renders itself and its children. Render order is children with z <= 0, itself, children with z > 0
> - .draw(self): draws itself
> - .transform(self): helper for draw and visit, handles the openGL coordinate changes needed to render mandated by position, anchor, scale and rotation
> - .camera: instead of the stock position, anchor, scale and rotation uses a custom gluLookAt that also allows z. This is rarely used, usually by special effects in transitions.

# Time management

> - .schedule_interval(self, callback, interval, *args, **kwargs): Schedule a function to be called every interval seconds.
> - .schedule(self, callback, *args, **kwargs): Schedule a function to be called every frame.
> - .unschedule(self, callback): Remove a function from the schedule.
> - .pause_scheduler(self): Time will stop passing for this node: scheduled callbacks will not be called, worker actions will not be called
> - .resume_scheduler(self): Time will continue/start passing for this node and callbacks will be called, worker actions will be called

# Automated changes along the time

> - .do(self, template_action, target=None): most of the time it is called with target=None, which result in target being self. The action will perform a potentially gradual change in target. Returns the worker action, which is the object doing the changes. You must save a reference to the worker if you plan to call later to .action_remove
> - .action_remove(self, worker_action): the worker action will terminate
> - .pause(self): Suspends the execution of actions.
> - .resume(self): Resumes the execution of actions.
> - .stop(self): All actions scheduled in this node are removed, stop method will be called for each action.
> - .are_actions_running(self): True or False

The automated changes are performed by a collaboration between CocosNode instances and Action instances.

Automated changes along the time snippets Also the test directory has many samples like test_accel*.py, test_blink.py, test_callfunc*.py and others.