eugene
eugene

Reputation: 41665

CCNode's position

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?

Upvotes: 1

Views: 2407

Answers (5)

Listen
Listen

Reputation: 151

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.

Upvotes: 0

CodeSmile
CodeSmile

Reputation: 64478

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.

Upvotes: 0

Nathanael Weiss
Nathanael Weiss

Reputation: 805

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.

Upvotes: 2

m.ding
m.ding

Reputation: 3182

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.

Upvotes: 0

Morion
Morion

Reputation: 10860

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)

Upvotes: 0

Related Questions