cocos2dx how to clear CCAnimationCache - cocos2d-iphone

In cocos2d-x, I cache the animation in CCAnimationCache. But how can I clear all the animation-cache?

You should try
CCAnimationCache::sharedAnimationCache()->purgeSharedAnimationCache();
It releases all the CCAnimation objects and the shared instance.
Else you can also use
removeAnimationByName("youAnimationName");
this will remove particular animation you wish.
Hope this helps you.. :)

In cocos2d, we use these calls...find same api in cocos2d-x
[[CCDirector sharedDirector] purgeCachedData];
[[CCTextureCache sharedTextureCache] removeAllTextures];
[CCTextureCache purgeSharedTextureCache];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
[CCSpriteFrameCache purgeSharedSpriteFrameCache];

Related

How can you show an MCBrowserViewController on a scene in cocos2d?

I'm porting a code to ios7 in wich I used old GKPeerPickerController and cocos2d to make a multiplayer game over Bluetooth.
In the new version, I'm using the MCBrowserViewController, but im unable to show it on the scene.
Im calling it with
[[[CCDirector sharedDirector] view] addSubview: browserVC];
where browserVC is an initiallized MCBrowserViewController but as this is only to show UIViews I get the error
Cannot initialize a parameter of type 'UIView *' with an rvalue of
type 'MCBrowserViewController *'
Is there any way to show the MCBrowserViewController on the scene?
Thanks in advance!
Should work
[[CCDirector sharedDirector] presentViewController:browserVC animated:YES completion:nil];

Cocos2d animations in main loop

Anyone that can give any hint of the smartest way to do a main loop animation? I don't want to use CCAnimation because I want to control the animations frame by frame.
Shall I store the sprite rect (relative to the sprite sheet) for each individual frame in an array, and then look up the suiting rect in each animation step? I tried to find out how this is done in CCAnimation, but I didn't succeed...
How to get the rect for each frame at initialization?
How to set the rect at each animation step?
Do I need to use CCSpriteBatchNode? I guess not, eh?
Cannot crealry understand, why you don't want to use CCAnimation, but anyway, to get answer for your questions you can check creation code of the CCSprite instance. Then, check creation of CCSpriteFrame instance. There you will find the answer for at least your first question.
Actually if you just want to manage animation frames differently from CCAnimate, you can just store array of CCSpriteFrames and show them as you want(in CCAnimate action these frames are just changed one by one in equal time intervals).
And if you do not want to show more than one frame of your animation, there is no difference will you use CCSpriteBatchNode or not. It saves a lot of processor time if you need to draw several parts of one texture, as it draws them in one draw call instead of send draw message to all of these sprites.
As you want animate sprite frame by frame I think using CCSpriteBatchNode would be a better option as it give you frame by frame access of animation.Making plist of sprites using any tool like "Zwoptex" will give an efficient way to animate using CCSpriteBatchNode.
Hope you know the animation using plist file with CCSpriteBatchNode.
I did the following with inspiration from Morions answer:
In the game tick function:
_animationFrames.legFrame = (_animationFrames.legFrame + 1) % _animationFrames.legFrames.count;
[_legs setDisplayFrame: [_animationFrames.legFrames objectAtIndex: _animationFrames.legFrame]];
And in the init function:
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode
batchNodeWithFile:#"Player.png"];
[self addChild:spriteSheet];
_animationFrames.legFrames = [[NSMutableArray array] retain];
for(int i = 0; i <= 15; ++i)
{
[_animationFrames.legFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"Player_legs-%d.png", i]]];
}
_legs = [CCSprite spriteWithSpriteFrameName:#"Player_legs-0.png"];
[_sprite addChild: spriteSheet];
[spriteSheet addChild:_legs z:1];

Adding multiple spritesheets in cocos2d

I have the following code to set up my spritesheets and batch node:
CGSize screenSize = [[CCDirector sharedDirector] winSize];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"soldier-test.plist"];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"soldier-running.plist"];
batchNode = [CCSpriteBatchNode batchNodeWithFile:#"soldier-test.png"];
self.player = [Player spriteWithSpriteFrameName:#"shooting s0000.bmp"];
[batchNode addChild:self.player];
[player setPosition:ccp(screenSize.width/2, screenSize.height/2)];
[self addChild:batchNode];
However, when I try to have player (a subclass of CCSprite) perform an action using frames from the second spritesheet, I get assertion errors related to the texture files. Do I need to combine the sheets into one, or is there a way to span one CCSprite over multiple spritesheets?
A SpriteBatchNode can only have children that all use the same texture. Your player needs to use the texture soldier-test.png if you want to add it to your batchNode.
With a TextureAtlas you can put multiple different textures into one big image.

Cocos2d - a way to get the current scene?

I'm looking for a way to get the current scene so that I'll be able to tell which scene is running at any time.
Thanks!
Check out CCDirector. You can get the running scene like this:
[[CCDirector sharedDirector] runningScene];
From the documentation of cocos2D:
-(CCScene*) runningScene [read, assign]
The current running Scene. Director can only run one Scene at the time
Sandro Meier

How to restart a level in an iPhone game

I want to restart the current scene i am on. I thought of using replaceScene and replace it with itself. Is that ok to do ?
Level2Scene *scene = [Level2Scene node];
[[CCDirector sharedDirector] replaceScene:scene];
Level2Scene is the current scene I am in.
Thanks
Abhinav
Using replaceScene: is a good way to restart the level, if levels are represented by scenes. Just make sure that if you have any global state (stored outside of the scene) that you reset that, too.