Class cocos.cocosnode.CocosNode

object --+
         |
        CocosNode

Cocosnode is the main element. Anything thats gets drawn or contains things that get drawn is a cocosnode. The most popular cocosnodes are scenes, layers and sprites.

The main features of a cocosnode are:

Some cocosnodes provide extra functionality for them or their children.

Subclassing a cocosnode usually means (one/all) of:

Methods

  __init__(self)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
  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.
  resume_scheduler(self)
Time will continue/start passing for this node and callbacks will be called.
  pause_scheduler(self)
Time will stop passing for this node and callbacks will not be called
  get_ancestor(self, klass)
Walks the nodes tree upwards until it finds a node of the class klass or returns None
  add(self, child, z=0, name=None)
Adds a child to the container
  remove(self, obj)
Removes a child from the container given its name or object
  get_children(self)
  __contains__(self, child)
  get(self, name)
Gets a child from the container given its name
  on_enter(self)
Called every time just before the node enters the stage.
  on_exit(self)
Called every time just before the node leaves the stage
  transform(self)
Apply ModelView transformations you will most likely want to wrap calls to this function with glPushMatrix/glPopMatrix
list walk(self, callback, collect=None)
Executes callback on all the subtree starting at self.
  visit(self)
This function visits it's children in a recursive way.
  draw(self, *args, **kwargs)
This is the function you will have to override if you want your subclassed to draw something on screen.
Action instance do(self, action, target=None)
Executes an action.
  remove_action(self, action)
Removes an action from the queue
  pause(self)
Suspends the execution of actions.
  resume(self)
Resumes the execution of actions.
  stop(self)
Removes all actions from the running action list
  are_actions_running(self)
Determine whether any actions are running.
Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties

Inherited from object: __class__

Instance Variables

  children
list of children.
  children_names
dictionary that maps children names with children references
  x
x-position of the object relative to its parent's children_anchor_x value.
  y
y-position of the object relative to its parent's children_anchor_y value.
  scale
a float, alters the scale of this node and its children.
  rotation
a float, in degrees, alters the rotation of this node and its children.
  camera
eye, center and up vector for the Camera.
  children_anchor_x
offset from (x,0) from where children will have its (0,0) coordinate.
  children_anchor_y
offset from (x,y) from where children will have its (0,0) coordinate.
  transform_anchor_x
offset from (x,0) from where rotation and scale will be applied.
  transform_anchor_y
offset from (0,y) from where rotation and scale will be applied.
  visible
whether of not the object is visible.
  grid
the grid object for the grid actions.
  actions
list of Action objects that are running
  to_remove
list of Action objects to be removed
  skip_frame
whether or not the next frame will be skipped
  scheduled_calls
list of scheduled callbacks
  scheduled_interval_calls
list of scheduled interval callbacks
  is_running
whether of not the object is running

Class Variables

  anchor = make_property("anchor")
Anchor point of the object.
  anchor_x = make_property("anchor_x")
Anchor x value for transformations and adding children
  anchor_y = make_property("anchor_y")
Anchor y value for transformations and adding children
  children_anchor = make_property("children_anchor")
Children anchor point.
  transform_anchor = make_property("transform_anchor")
Transformation anchor point.
  parent = property(_get_parent, _set_parent, doc= '''The parent...
  position = property(_get_position, _set_position, doc= '''The ...

Method Details

__init__

(Constructor) __init__(self)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides:
object.__init__

schedule_interval

schedule_interval(self, callback, interval, *args, **kwargs)

Schedule a function to be called every interval seconds.

Specifying an interval of 0 prevents the function from being called again (see schedule to call a function as often as possible).

The callback function prototype is the same as for schedule.

This function is a wrapper to pyglet.clock.schedule_interval. It has the additional benefit that all calllbacks are paused and resumed when the node leaves or enters a scene.

You should not have to schedule things using pyglet by yourself.

Parameters:
callback : function
The function to call when the timer lapses.
interval : float
The number of seconds to wait between each call.

schedule

schedule(self, callback, *args, **kwargs)

Schedule a function to be called every frame.

The function should have a prototype that includes dt as the first argument, which gives the elapsed time, in seconds, since the last clock tick. Any additional arguments given to this function are passed on to the callback:

def callback(dt, *args, **kwargs):
    pass

This function is a wrapper to pyglet.clock.schedule. It has the additional benefit that all calllbacks are paused and resumed when the node leaves or enters a scene.

You should not have to schedule things using pyglet by yourself.

Parameters:
callback : function
The function to call each frame.

unschedule

unschedule(self, callback)

Remove a function from the schedule.

If the function appears in the schedule more than once, all occurances are removed. If the function was not scheduled, no error is raised.

This function is a wrapper to pyglet.clock.unschedule. It has the additional benefit that all calllbacks are paused and resumed when the node leaves or enters a scene.

You should not unschedule things using pyglet that where scheduled by node.schedule/node.schedule_interface.

Parameters:
callback : function
The function to remove from the schedule.

add

add(self, child, z=0, name=None)
Adds a child to the container
Parameters:
child : object
object to be added
z : float
the z index of self
name : str
Name of the child

remove

remove(self, obj)
Removes a child from the container given its name or object
Parameters:
obj : string or object
name of the reference to be removed or object to be removed

get

get(self, name)
Gets a child from the container given its name
Parameters:
name : string
name of the reference to be get

walk

walk(self, callback, collect=None)
Executes callback on all the subtree starting at self. returns a list of all return values that are not none
Parameters:
callback : function
callable, takes a cocosnode as argument
collect : list
list of visited nodes
Returns:
list: the list of not-none return values

visit

visit(self)

This function visits it's children in a recursive way.

It will first visit the children that that have a z-order value less than 0.

Then it will call the draw method to draw itself.

And finally it will visit the rest of the children (the ones with a z-value bigger or equal than 0)

Before visiting any children it will call the transform method to apply any possible transformation.

draw

draw(self, *args, **kwargs)

This is the function you will have to override if you want your subclassed to draw something on screen.

You must respect the position, scale, rotation and anchor attributes. If you want OpenGL to do the scaling for you, you can:

def draw(self):
    glPushMatrix()
    self.transform()
    # ... draw ..
    glPopMatrix()

do

do(self, action, target=None)
Executes an action. When the action finished, it will be removed from the sprite's queue.
Parameters:
action : an Action instance
Action that will be executed.
Returns:
Action instance: A clone of action

remove_action

remove_action(self, action)
Removes an action from the queue
Parameters:
action : Action
Action to be removed

Instance Variable Details

children

list of children. each item is (int, child-reference) where int is the z-order

x

x-position of the object relative to its parent's children_anchor_x value. Default: 0

y

y-position of the object relative to its parent's children_anchor_y value. Default: 0

scale

a float, alters the scale of this node and its children. Default: 1.0

rotation

a float, in degrees, alters the rotation of this node and its children. Default: 0.0

camera

eye, center and up vector for the Camera. gluLookAt() is used with these values. Default: FOV 60, center of the screen. IMPORTANT: The camera can perform exactly the same transformation as scale, rotation and the x, y attributes (with the exception that the camera can modify also the z-coordinate) In fact, they all transform the same matrix, so use either the camera or the other attributes, but not both since the camera will be overridden by the transformations done by the other attributes. You can change the camera manually or by using the Camera3DAction action.

children_anchor_x

offset from (x,0) from where children will have its (0,0) coordinate. Default: 0

children_anchor_y

offset from (x,y) from where children will have its (0,0) coordinate. Default: 0

transform_anchor_x

offset from (x,0) from where rotation and scale will be applied. Default: 0

transform_anchor_y

offset from (0,y) from where rotation and scale will be applied. Default: 0

visible

whether of not the object is visible. Default: True

grid

the grid object for the grid actions. This can be a Grid3D or a TiledGrid3D object depending on the action.

Class Variable Details

anchor

Anchor point of the object. Children will be added at this point and transformations like scaling and rotation will use this point as the center
Value:
make_property("anchor")

children_anchor

Children anchor point. Children will be added relative to this point
Value:
make_property("children_anchor")

transform_anchor

Transformation anchor point. Transformations like scaling and rotation will use this point as it's center
Value:
make_property("transform_anchor")

parent

Value:
property(_get_parent, _set_parent, doc= '''The parent of this object.

    :type: object
    ''')

position

Value:
property(_get_position, _set_position, doc= '''The (x, y) coordinates \
of the object.

    :type: (int, int)
    ''')