Cocos2d level hierarchy and drawing order

I ran into a layer hierarchy issue in cocos2d.

I have a character with sprites for his body parts. The parent sprite of the body is the torso. Then I have hands and a head that are his daughters, then elbows and hands are children of hands. It works very well: I turn my elbow, and my hand turns with him, like on a real puppet.

The problem is when I want to make her worry, her hands drop behind her head, since the head has a higher z-order than the hands (which are the parents of the hands).

So, I added another arm as a child of a torso and turned its opacity ON and OFF depending on whether I need a hand above or behind my head. However, the other hand is not a child of the hand, it is a child of the torso and does not rotate whenever the arm rotates. So, I need to put it manually in each frame.

So my question is: is it possible for the parent sprite of one node to inherit its translation, but draw it over another specified sprite? (e.g., above the other brother of his parent)

Changing the z-order of the hands and head is out of the question, since the animation is exported from another program that does not support these parameters.

Thanks!

+1
source share
4 answers

How is "animation" exported from another program? I think this will be a problem. If I did, I would use the Z layer. Better yet, look at Sprite Atlases to create the animation.

I think you do too much to create an animation. With Atlas, you will create an animation for a figure that raises its arms and waves them. and then just call the animation on the sprite atlas.

+2
source

I solved the same problem in my application by adding an additional invisible trunk sprite to the model. in this case, you can attach one shoulder and head to an invisible torso, and the other to the second body

+1
source

I think I found a solution to my problem. This is a property of the Z node vertex.

/** The real openGL Z vertex. Differences between openGL Z vertex and cocos2d Z order: - OpenGL Z modifies the Z vertex, and not the Z order in the relation between parent-children - OpenGL Z might require to set 2D projection - cocos2d Z order works OK if all the nodes uses the same openGL Z vertex. eg: vertexZ = 0 @warning: Use it at your own risk since it might break the cocos2d parent-children z order @since v0.8 */ @property (nonatomic,readwrite) float vertexZ; 


This article describes how to use it.

0
source

Also make sure you initiate an EAGLView with depth enabled

 EAGLView *glView = [EAGLView viewWithFrame: [window bounds] pixelFormat: kEAGLColorFormatRGBA8 depthFormat: GL_DEPTH_COMPONENT16_OES]; 
0
source

Source: https://habr.com/ru/post/923992/


All Articles