Hi,

When I learned cocos, I was surprised there wasn't a z attribute to nodes. 
And in one project I had a node with many many childs, and remove() 
performance was bad (it has to go through all childen list, even though it 
is ordered). I did some modifications to cocosnode.py, so that they have a 
z attribute, modifiable at runtime.
For backward compatibility, CocosNode.add function can still use a z 
parameter, which changes the z attribute of child. Here is a patch and a 
small demo :
http://pastebin.com/YbbE7YEm

There might be some things I didn't think about, but this seems to work.

-- 
You received this message because you are subscribed to the Google Groups 
"cocos2d discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cocos-discuss.
For more options, visit https://groups.google.com/groups/opt_out.
--- /usr/local/lib/python2.7/dist-packages/cocos2d-0.5.5-py2.7.egg/cocos/cocosnode.py	2012-09-08 15:40:43.000000000 +0200
+++ /home/corentin/Documents/Aptana Studio 3 Workspace/cocosnode_z/src/cocos/cocosnode.py	2013-11-18 21:58:57.612970365 +0100
@@ -89,6 +89,10 @@
         #: y-position of the object relative to its parent's children_anchor_y value.
         #: Default: 0
         self._y = 0
+        
+        #: z-position of the object, for drawing order.
+        #: Default : 0
+        self._z = 0
 
         #: a float, alters the scale of this node and its children.
         #: Default: 1.0
@@ -153,7 +157,19 @@
         self.is_inverse_transform_dirty = False
         self.inverse_transform_matrix = euclid.Matrix3().identity()
 
-
+    @property
+    def z(self):
+        return self._z
+    
+    @z.setter
+    def z(self, value):
+        if self.is_running :
+            self.parent.remove(self)
+            self._z = value
+            self.parent.add(self)
+        else :
+            self._z = value
+    
     def make_property(attr):
         def set_attr():
             def inner(self, value):
@@ -393,7 +409,7 @@
     rotation = property( _get_rotation, lambda self, angle: self._set_rotation(angle))
 
 
-    def add(self, child, z=0, name=None ):
+    def add(self, child, z=None, name=None ):
         """Adds a child and if it becomes part of the active scene calls its on_enter method
 
         :Parameters:
@@ -418,8 +434,9 @@
             self.children_names[ name ] = child
 
         child.parent = self
-
-        elem = z, child
+        if z :
+            child.z = z
+        elem = child.z, child
         bisect.insort( self.children,  elem )
         if self.is_running:
             child.on_enter()
@@ -455,10 +472,10 @@
             self._remove(obj)
 
     def _remove( self, child ):
-        l_old = len(self.children)
-        self.children = [ (z,c) for (z,c) in self.children if c != child ]
-
-        if l_old == len(self.children):
+        i = bisect.bisect_left(self.children, (child.z, child))
+        if i !=len(self.children) :
+            del self.children[i]
+        else :
             raise Exception("Child not found: %s" % str(child) )
 
         if self.is_running:

Reply via email to