How to put animation between frames spritesheet in cocos2d? - iphone

I am new to cocos2d .Can any one suggest me a method to give animation for the transition between one frame to another frame.
this is my code to fetch frames and just display
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"spritesheet.plist"];
// Create a sprite sheet with the Happy Bear images
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"spritesheet.png"];
[self addChild:spriteSheet];
// Load up the frames of our animation
walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <=7; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"sprite%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
// Create a sprite for our bear
CGSize winSize = [CCDirector sharedDirector].winSize;
self.sprite = [CCSprite spriteWithSpriteFrameName:#"sprite.png"];
self.sprite.position=ccp(320,480);
self.sprite.anchorPoint=ccp(1,1);
self.walkAction = [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO];
How to do the animation for the transition between frames
thanks in advance

You can not "transition" (morph) between two frames (images), if that's what you mean.
It sounds to me like you want something that's called keyframe interpolation, which is normally used in 3D animations. A 2D animation displays one frame after another. You can not interpolate between two frames graphically (morphing), you can only interpolate properties like position.

Related

What is different between CCAnimate and CCAnimation?

I created a new project with cocos2d, and then create a layer and a sprite.
I am trying to apply an animation to my sprite, and I found some samples about that. But I don't understand the difference between CCAnimation and CCAnimate, so I haven't applied any animation for my sprite yet.
What is the difference between these two and what is the real meaning of them? How to use them?
Can any one explain me about that? Please help me.
Here is code from "Working with Sprites, and Sprite batch nodes" in cocos2d-iphone Programming Guide:
......
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 15; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"grossini_dance_%02d.png",i]];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithName:#"dance" delay:0.2f frames:animFrames];
[sprite runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
As you can see, you need to use both CCAnimate and CCAnimation to run a CCSpriteFrame-based animation on the sprite. Per description of CCAnimation:
The CCAnimation object contains CCSpriteFrame objects, and a possible delay between the frames. You can animate a CCAnimation object by using the CCAnimate action. Example:
[sprite runAction:[CCAnimate actionWithAnimation:animation]];
Generally speaking, CCAnimation is a wrapper of frame container. CCAnimate is the action that the sprite need to do - in fact it is a subclass of CCAction, which represents an action that a sprite can "run", such as moving, jumping, rotating, etc. You can use its other subclasses to do some kinds of animation on your sprite without creating the frames yourself. Have a try with something like CCFadeIn, CCRotateBy, etc.

Parallax Scrolling for the Endless Game background using cocos2D

I am newbie programmer in cocos2D.... I wanted to create the Endless Background using the Parallax Scrolling.. I am trying to learn the Parallax but it doesn't update with the time and sprites doesn't keep on rotating from top to bottom! The snippet i tried is as below
-(id) init
{
if((self = [super init]))
{
CGSize wSize = [CCDirector sharedDirector].winSize;
self.isTouchEnabled = YES;
CGPoint topOffset = CGPointMake(wSize.width, 0);
CGPoint midOffset = CGPointMake(wSize.width/2,0);
CGPoint downOffset = CGPointZero;
CCSprite *para1 = [CCSprite spriteWithFile:#"Default.png"];
CCSprite *para2 = [CCSprite spriteWithFile:#"Icon.png"];
CCSprite *para3 = [CCSprite spriteWithFile:#"Default.png"];
CCSprite *para4 = [CCSprite spriteWithFile:#"Icon.png"];
paraNode = [CCParallaxNode node];
[paraNode addChild:para1 z:1 parallaxRatio:CGPointMake(0.5f, 0)
positionOffset:topOffset];
[paraNode addChild:para2 z:2 parallaxRatio:CGPointMake(1, 0) positionOffset:topOffset];
[paraNode addChild:para3 z:4 parallaxRatio:CGPointMake(2, 0) positionOffset:midOffset];
[paraNode addChild:para4 z:3 parallaxRatio:CGPointMake(3, 0) positionOffset:downOffset];
[self addChild:paraNode z:0 ];
[self scheduleUpdate];
}
return self;
}
-(void) update : (ccTime) dt
{
//Need to move the Parallax Node with the repetition of the background
}
This is the Implementation file... I am stuck here for moving the contiunous moving of the background in the Horizontal or Portrait mode.
Thanks for the Help in Advance
CCParallaxNode doesn't support endless scrolling, unless you modify its code. I have an example for endless parallax scrolling in my Learn Cocos2D book. From that link you can also download the book's source code where you'll find the parallax class in chapters 6 to 8.
I have created a simple class named ParallaxManager which is capable for creating infinite parallax effect for both small sprite like small cloud
as well large layer sprite like grass.
You can find out the complete project from GitHub.

How to set the frame of an animation in cocos2d

I have made a CCAnimationHelper class that performs the following. You give it a fileName, a frameCount, and a Delay and it will give you back an animation. The thing I want to know is how to set the frame of the animation so instead of starting at frame 1, it will start at frame 10 instead. Here is my code
// Creates an animation from sprite frames.
+(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay
{
// load the ship's animation frames as textures and create a sprite frame
NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
for (int i = 1; i < frameCount; i++)
{
NSString* file = [NSString stringWithFormat:#"%#%i.png", frame, i];
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCSpriteFrame* frame = [frameCache spriteFrameByName:file];
[frames addObject:frame];
}
// return an animation object from all the sprite animation frames
return [CCAnimation animationWithFrames:frames delay:delay]; //Is there another method I should be using here to set the frame
}
There isn't a built in funcion that does that, but you can simply create your animation and pass it the NSArray starting at the frame you want:
CCAnimation *animation = [CCAnimation animation];
NSArray *offset = [NSArray arrayWithObjects:frame3, frame4, nil];
[animation initWithFrames:offset];

Cocos2d animation from sprite sheet

So i have the action build to make my frog jump but i have 5 images and i want to run them in the order 1,2,3,4,5,4,3,2,1 using the coordinates. I only want to loop them once everytime the from jumps.
Thanks
while defining your image you can use the following code:
CCAnimation* anim = [CCAnimation animationWithName:#"frog"];
CCSpriteFrame* frames[5];
CGRect Rect;
// some code to initialize Rect position
for(int i=0;i<5;i++)
{
frames[i] = [CCSpriteFrame frameWithTexture:#"frog.png" rect:Rect];
// some code to move Rect so that it will mark next frame;
}
for(int i=0;i<5;i++)
[anim addFrame:frames[i]];
for(int i=3;i>=0;i--)
[anim addFrame:frames[i]];
use ccanimation and ccspritebatchnodefile
save your image sequence in spritesheet

Multiple animations and textures with Cocos2d - How to remove texture from memory?

I am writing an application where you push different buttons and a character gets animated. The thing is that I have many images, so I need to use one texture for each animation. Therefore I need to release sprite sheet and frame cash, but it does not seem to be working. Memory gets allocated more and more until the app crashes. Here is the code:
// **** DEFINE THE ANIMATION - EATING 1: ****
// Create a sprite sheet with all the images
CCSpriteSheet *spriteSheet = [CCSpriteSheet spriteSheetWithFile:#"Eating4.png"];
// This loads an image of the same name (but ending in png), and goes through the
// plist to add definitions of each frame to the cache.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"Eating4.plist"];
[self addChild:spriteSheet];
///[self addChild:spriteSheet2];
// Load up the frames of our animation
NSMutableArray *eating1AnimFrames = [NSMutableArray array];
for(int i = 0; i <= 50; ++i) {
if (i<=9){
[eating1AnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"Eating4_000%d.png", i]]];
}
else if (i>9) {
[eating1AnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"Eating4_00%d.png", i]]];
}
}
CCAnimation *eatingAnim = [CCAnimation animationWithName:#"eating" delay:0.05f frames:eating1AnimFrames];
// Create a sprite for the mouse
CGSize winSize = [CCDirector sharedDirector].winSize;
self.mouse = [CCSprite spriteWithSpriteFrameName:#"Eating4_0000.png"];
_mouse.position = ccp(winSize.width/2+20, winSize.height/2);
// Adjust the size of the Sprite:
[_mouse setScaleX: 1];
[_mouse setScaleY: 1];
//self.eatingAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:eatingAnim restoreOriginalFrame:NO]];
self.eatingAction = [CCAnimate actionWithAnimation:eatingAnim restoreOriginalFrame:NO];
[spriteSheet addChild:_mouse];
[_mouse runAction:_eatingAction];
I try to release memory like this:
[[CCTextureCache sharedTextureCache] removeAllTextures];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
[[CCTextureCache sharedTextureCache] removeAllTextures];
You don't want to do this! This removes all textures from memory, then reloads all of them that are still in use or the next time they are used. This causes a significant overhead and it's generally bad for your game's performance.
Instead, remove only the texture you need to remove by calling:
[[CCTextureCache sharedTextureCache] removeTexture:tex];
For this to work, you will also have to remove (release) all references to the animation from the Cocos2D node hierarchy.
Calling removeUnusedTextures would be a better choice for clearing up memory used by unused textures:
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
Or you can ask use CCDirector::purgeCachedData that does the same thing internally:
[[CCDirector sharedDirector] purgeCachedData];