CCNode's position - cocos2d-iphone

This post discusses cons/pros of subclassing CCSprite or having CCSprite as member.
Suppose I have a CCNode which has a CCSprite as a member.
Then I'll add the CCNode as a child to my layer
and add CCSprite to the CCNode.
Now I want to change the position of CCSprite, should I change the position of the CCNode which contains the CCSprite or CCSprite itself?
If I change CCSprite's position, what happens to the CCNode's position?
edit
Is this considered an acceptable practice?
Am I better off setting CCNode's position and let CCSprite be positioned wherever CCNode is?

Adding a CCSprite as a child of a CCNode can be helpful when you have multiple sprites that you want to move along with the node.
Consider the example of a player sprite. Say you want to put a shadow underneath the player. Adding both the shadow sprite and the player sprite to a parent CCNode allows them both to be moved easily.
If you have a single sprite that has no other sprites you want to move along with it, then you don't need the CCNode parent.

nothing will be happened with your sprite. if you do not set content size and anchor point to your sprite's container, position of the node always will be equal to the position of node's (0.f, 0.f). so, position of your sprite will be related to it's parent (0.f, 0.f)

Basically, all Node in cocos2d-x have a parent Node (except Scene).
Changing the position of a parent Node will apply the position changes to all its child,
Changing the position of a Child Node will not affect anything unless this Child Node has its own children.
In your Example, If you change the CCSprite position, the position of CCNode will remain the same. But, if you changed the position of your CCNode, the same changes(shifting on the screen) will happen on the Sprite.

You want to set the sprite's position. The simple reason being that the node may contain other child nodes which should be able to move independently from the sprite.
Changing a node's position does not affect the parent's position at all. Likewise, changing a parent's position does not change the child node positions at all. Child node positions are an offset to their parent position, so if you move the parent they will follow along but their position property (which is relative to the parent position) remains the same.

You'd better move the CCNode's position cause the position of the CCSprite is also affected by its parent.
Although by moving the CCSprite's position you'll get the same result visually(its parent is not moved), it makes getting the position of a sprite more complicated and confusing.

Related

COCOS2DX, Adding Sprite To Parent

I have created the ball sprite as a parent sprite and i need to add a child sprite in all the boundary of the parent sprite. by defaultly the parent image png it take as square shape, But i want to add like circle. What i can do in COCOS2d-X to add child around circle boundry?
Try drawing a circle, you can use this post as reference. This drawn circle has a perfect round boundary. Attach that to your parent circle image (the one with square boundaries) and set it to invisible. Now you can use attach the child sprites to that invisible drawn circle. It will have the illusion that the child sprites are attached to the circle image.

Move sprite to top of all elements - Cocos2d

I know how to position a CCSprite in relation to all children nodes based on
sprite.zOrder = 5;
However, I have a CCNode that is added to the CCScene and I need a child of that CCNode to be positioned above all the elements in the CCScene. Is this possible?
You can perhaps go for manually setting the vertexZ property. Its hidden so you will have to #import CCNode_Private.h. This will break the parent children ordering the zOrder enforces but perhaps that is what you want.
You will also need to enable depth buffer CCSetupDepthFormat: #(GL_DEPTH_COMPONENT24_OES)

How can I display the anchorpoint for a CCNode in cocos2d?

Is there a way to display a CCNode's anchor point? This would be very useful for debugging.
Not built-in, but you could draw a point or circle at the anchor point location using the anchorPointInPoints property.
-(void) draw
{
[super draw];
ccDrawCircle(self.anchorPointInPoints, 20, 0, 8, YES);
}
Of course, I always recommend not to change the anchorPoint in the first place. The alternative is to add the node to a parent node, offset it from the parent, and then the parent's position acts like the anchorpoint for the child node. The advantage is that methods like boundingBox aren't offset from the node's position (can be an issue for hit detection), and you can rotate the child node around its center point and around its parent.
You can access the anchor point of a CCNode with
- (CGPoint) anchorPointInPixels
which is a readonly method. Afterwards, you have several ways of actually marking the spot. You could use
- ccDrawCircle()
while overriding the draw method or alternatively put up a texture on that point, if you want something fancier.

Cocos2d: Change sprite's zOrder dynamically (using CCSpriteBatchNode)

Is there a way to change the zOrder of a sprite rendered by CCSpriteBatchNode? I've tried it like this:
[self reorderChild:mySprite z:indexOfAnArray];
I get this error: 'If CCSprite is being rendered by CCSpriteBatchNode, CCSprite#draw SHOULD NOT be called'
I also tried with the zOrder property of the sprite but unfortunately is read-only.
I need to change the zOrder because, depending on the position where my sprite will appear, the zOrder needs to be changed.
self is probably not the parent of sprite. Remember that the parent is the CCSpriteBatchNode that holds it, and self is, from your point of view, probably the scene.

How to retrieve absolute position of child sprite (that is child of a parent sprite) in Cocos2d

I have two parent sprites.
Each parent has two children sprites.
Although the two parent sprites have different positions, it seems that the position of the child sprites for both parents are identical.
What I mean is, although the children are attached to the parent, and visually appear to move wherever the parents move, the point value of the "position" property remains the same.
When you move the parents around the screen, you can see the children moving too. There position really does change.
But the "position" property of the children always remains the same.
What am I doing wrong? How can I retrieve the absolute position of the child sprite?
To get one child's absolute position, relative to the screen (or more accurately the scene), use this:
CGPoint absolutePosition = [childSprite convertToWorldSpace:childSprite.position];
I don't think you were doing anything wrong. A child's position is relative to its parent so it makes sense that the children's positions are not changing.