I made a new .ccb file on SpriteBuilder where I created an animated CCSprite with physicsEnabled and a few physics properties. At a certain point I would like to draw the animated CCSprite on a menu. I don't want to make a copy of the whole .ccb file because now I don't need the physics part I just want to copy the CCSprite and the animation (which is a set of keyframes). What's the best way to achieve this?
1 Answers
You just need to make a new class in Xcode, and then make a new object of that class and add it to the menu. I explain the steps:
Go to that new .ccb file in SpriteBuilder and select the content Node of the CCSprite. (It depends on what kind of .ccb file you created, scene,sprite,node...) Then go to Code Connections and select a name for Custom Class, for example MyAnimatedSprite.
So now go to Xcode, and then make a new file in your project with type CCNode, and name it 'MyAnimatedSprite'.
Now the final step is to add this sprite to the menu.
You must have a Physic Node previously added to the menu .ccb file and also declared in the .m file, because you said your sprite has physics enabled. If you don't have it, simply add it with sprite builder wherever you want, and go to Code Connections and select under custom class, Doc Root Var and call it for this example, '_physicsNode'
Now go to the Menu class in Xcode, and simply add the sprite class to the menu with these lines:
@implementation Menu{
..... YOUR CODE .....
CCPhysicsNode *_physicsNode; //add physics node if you don't have it
}
-(void) didLoadFromCCB{
//create a new object of type MyAnimatedSprite
CCNode *myAnimatedSprite = [CCBReader load:@"MyAnimatedSprite"];
...YOUR CODE....
}
And you will have your sprite in your menu working perfectly :)

- 586
- 5
- 24
-
Isn't it a bit of a over kill to use a physicsnode where I don't want to compute any physics? – João Abrantes Apr 28 '14 at 08:53
-
You said your CCSprite has physics enabled, so if you are adding it to the Menu, there must be a PhysicsNode so you can add the sprite. Otherwise it will crash – mursang Apr 28 '14 at 09:08
-
yes but I would like to disable its physics and then copy the animated sprite. Isn't it possible? – João Abrantes Apr 28 '14 at 17:57
-
As I know, you can't delete them. You can disable them, so physics exist but disabled. So you need to create the PhysicsNode, add your sprite with physics, and then disable physics by doing: " myAnimatedSprite.physicsBody.sensor = TRUE; " I don't know any other way – mursang Apr 28 '14 at 19:15
-
You could just try to add the physics node in code, assuming that it is a simple object. – Tibor Udvari Apr 30 '14 at 08:50