Cocos2d Sprite linking
If this question is a duplicate, plz correct me. What I want to do is to have a character on the screen who may hold a gun. So if the background move, the character should move with it. And if the character move, the gun should move with him/her.
Does anyone know how to do this? Sorry for being a newbie
You can use a CCLayer for what you would like to do.
A CCLayer is a sort of "container" for all the sprites you want to control as a group. E.g., you create two CCSprites and add them to a CCLayer; you can define the sprites position in coordinates that are relative to the CCLayer and when the CCLayer moves, the sprites go with it.
You can also have multiple layers, for different groups of objects. E.g., if you have more characters, they could all go into the same layer.
A CCLayer is derived from CCNode, so you could do pretty much the same by adding your character, its weapon, and the background to the same node; but I would suggest to have a node for the two sprites (character and weapon), and a layer for the background sprite and the character+weapon node.
A CCLayer, in comparison to a CCNode, adds the possibility of handling the accelerometer (if your want your character follow it) and touches.
You can instantiate a new CCNode and call it's addChild method. Usually you would subclass CCNode and call it Character or something. The parent CCNode would contain all the sprite and they would move with it. CCSprite is a subclass of CCNode as well.
CCNode *character = [[[CCNode alloc] init] autorelease]; [character addChild:[CCSprite spriteWithFile:@"body.png"]]; [character addChild:[CCSprite spriteWithFile:@"gun.png"]];