cocos2d animation sprite - iphone

Hi I'm making a game for iphone and when the sprite moves I want it to change image between 3 images so it looks like it's running.
I'm using cocos2d right now and I'm pretty new to cocos2d. I know how to do this with cocoa but that doesn't work with cocos2d.
So my question is how do I change the sprite image between 3, images and I want to loop it while I'm holding my finger on a position on the screen?
Thanks in advance.

This is a pretty loaded question for being new to cocos2d.
I would work on the infinite animation first. Get that working and then work on pausing, resuming, and flipping the animation.
You can set up the animation in the same method that you are adding the sprite.
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i <= 3; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"Sprite-%d.png",i]];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithName:#"run" delay:0.1f frames:animFrames];
[mySprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
If you are unfamiliar with sprite sheets, there are plenty of free resources for creating a sprite sheet and plist (TexturePacker has a nice interface)
If you have trouble getting this to work, Ray Wenderlich has good tutorials. If you get this far here are some pointers for pausing, resuming, and flipping the animation
For pausing or resuming
[mySprite pauseSchedulerAndActions];
[mySprite resumeSchedulerAndActions];
Flip the animation whenever the touch directions switches horizontal directions
mySprite.flipX = YES;
mySprite.flipX = NO;

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"animations/grossini.plist"];
CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:#"grossini_dance_01.png"];
sprite.position = ccp( 100, 100);
CCSpriteSheet *spritesheet = [CCSpriteSheet spriteSheetWithFile:#"animations/grossini.png"];
[spritesheet addChild:sprite];
[self addChild:spritesheet];
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" frames:animFrames];
// 14 frames * 0.2sec = 2,8 seconds
[sprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithDuration:2.8f animation:animation restoreOriginalFrame:NO] ]];

Related

How to add Animated Image In Cocos2D without use of TexturePacker?

I am New at iOS Development.
I am also starting to learn Cocos2D.
I've read this tutorial: http://www.raywenderlich.com/tutorials#cocos2d
It is a superb tutorial for beginners, but I'm also interested in animating the image. How can I accomplish animation?
So I read tutorial (describe from Above Link) about how to put animated image with simple Project.
In This tutorial I used TexturePacker and it's working... but I want to know more about how to animate images without using TexturePacker.
Is it possible? If so, then please explain how or link to tutorials on how to make it work.
Thanks in advance.
You can run animation from file.
CCSprite *coin = [CCSprite spriteWithFile:#"MyImage_1.png"];
coin.position = ccp(mS.width*0.5f, mS.height*0.5f);
[self addChild:coin z:2];
{
NSString *animationName = #"UNIQUE_ANIMATION_NAME";
CCAnimation* animation = nil;
animation = [[CCAnimationCache sharedAnimationCache] animationByName:animationName];
if(!animation)
{
NSMutableArray *animFrames = [NSMutableArray array];
for( int i=1;i<=5;i++)
{
NSString* path = [NSString stringWithFormat:#"MyImage_%d.png", i];
CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:path];
CGSize texSize = tex.contentSize;
CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:tex rect:texRect];
[animFrames addObject:frame];
}
animation = [CCAnimation animationWithSpriteFrames:animFrames];
animation.delayPerUnit = 0.175f;
animation.restoreOriginalFrame = YES;
[[CCAnimationCache sharedAnimationCache] addAnimation:animation name:animationName];
}
if(animation)
{
CCAnimate *animAction = [CCAnimate actionWithAnimation:animation];
[coin runAction:animAction];
}
}

Playing png sequence

I am trying to play a sequence of PNGs in my application using the following code:
animatedSprite = [CCSprite spriteWithFile:#"Anim_1.png"];
animatedSprite.position = ccp( 512, 435 );
[self animatedSprite z:5];
NSArray *animFrames = [[NSArray alloc] initWithObjects:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"Anim_1.png"],
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"Anim_2.png"], nil];
spriteAnim = [CCAnimation animationWithSpriteFrames:animFrames delay:1.0f/24.0f];
id animAction = [CCAnimate actionWithAnimation:spriteAnim];
[spriteAnim runAction:animAction];
the 'animatedSprite' is added correctly but the animation doesn't play. What am I missing here?
I am using Cocos2d 2.0, xcode 4.5
You should run the action on the sprite, not the animation.
[animatedSprite runAction:animAction];

how to animated sprite for cocos2d in ccz format

According to this tutorial PVR images seem to be the best format for iOS sprites. However after creating a sprite sheet with Texturepacker and exporting out to this format I cannot get the animation to work in cocos2d.
According to the documentation here I should be using
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"LuckyCompiled.plist"];
But neither the tutorial nor the documentation explain how to do animation, except for this. Which is what the code below is based on.
But this only places the base image onto the layer and does not animate.
CCSprite *sprite = [[CCSprite alloc]init];
sprite.position = ccp(player.contentSize.width/2+40, winSize.height/2+40);
// Obtain the shared instance of the cache
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
// load the frames
[cache addSpriteFramesWithFile:#"LuckyCompiled.plist"];
// It loads the frame named "frame1.png".
// IMPORTANT: It doesn't load the image "frame1.png". "frama1.png" is a just the name of the frame
CCSpriteFrame *frame = [cache spriteFrameByName:#"lucky1.png"];
[sprite setDisplayFrame:frame];
[self addChild:sprite];
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 10; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"lucky%d.png",i]];
[animFrames addObject:frame];
}
NSLog(#"animaframes %#",animFrames);
CCAnimation *animation = [CCAnimation animationWithSpriteFrames:[NSArray arrayWithArray:animFrames]];
[sprite runAction:[CCAnimate actionWithAnimation:animation]];
Answer:
Needed to have a delay otherwise animation wasnt noticeable
[CCAnimation animationWithSpriteFrames:[NSArray arrayWithArray:animFrames]];
should have been (also no need to make is nsarray, mutable is fine)
[CCAnimation animationWithSpriteFrames:frames delay:0.1f];
Here is the code that I tried, it works fine.
CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:#"walkFrames.plist"];
player = [CCSprite spriteWithSpriteFrameName:#"f1.png"];
NSMutableArray *frames = [NSMutableArray arrayWithCapacity:8];
for (int i = 1; i < 9; i++) {
NSString *file = [NSString stringWithFormat:#"f%d.png", i];
CCSpriteFrame *frame = [frameCache spriteFrameByName:file];
[frames addObject:frame];
}
CCAnimation *walkAnim =[CCAnimation animationWithSpriteFrames:frames delay:0.1f];
CCAnimate *animate = [CCAnimate actionWithAnimation:walkAnim];
CCRepeatForever *rep = [CCRepeatForever actionWithAction:animate];
player.position = ccp(23, 285);
[player runAction:rep];
[self addChild:player];

sprite animation sheet

I'm developing an app using cocos2d and box2d phisycs. I whant to make my sprite animate on moving. I made a *.plist and *.png files in Zwoptex, and added them to my project. Now, I'm trying to create a sprite:
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:#"SquirrelAnimation.plist"];
node = [CCSpriteBatchNode batchNodeWithFile:#"SquirrelAnimation.png" capacity:100];
spriteTexture = [node texture];
b2BodyDef bodyDef;
bodyDef.type = bodyType;
CGSize size = [CCDirector sharedDirector].winSize;
CGPoint point = ccp(size.width / 2, size.height / 2);
bodyDef.position.Set(point.x / PTM_RATIO, point.y / PTM_RATIO);
body = world->CreateBody(&bodyDef);
sprite = [PhysicsSprite spriteWithTexture:spriteTexture];
[sprite setPhysicsBody:body];
[node addChild:sprite];
but this code makes one sprite with all frames to node. What am I doing wrong?
Extract frame like this...
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"spritesheet.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"spritesheet.png"];
[self addChild:spriteSheet];
NSMutableArray *frames = [[[NSMutableArray alloc]init]retain];
for(int i = 1; i <= numberFrames; i++) {
[frames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"%#0%d.png", file_name, i]]];
}
// Animation object with 0.04 seconds between each frame (~30fps)
CCAnimation *anim = [CCAnimation animationWithFrames:frames delay:0.04f];
if(self.sprite){
// Animate the sprite
[self.sprite runAction:[CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO]];
}

Animation in Cocos2d.

I am trying to make a sprite animate in cocos2D. I believe I have the animation set up, but how do I draw the animating sprite onto the screen? Here is what I have:
id anim = [[[CCAnimation alloc] initWithName:#"char_walking" delay:1/12.0] autorelease];
[anim addFrame:#"run2.png"];
[anim addFrame:#"run1.png"];
[anim addFrame:#"run3.png"];
[anim addFrame:#"run4.png"];
[anim addFrame:#"run3.png"];
[anim addFrame:#"run1.png"];
id myAction = [CCAnimate actionWithAnimation:anim];
id repeating = [CCRepeatForever actionWithAction:myAction];
[character do:repeating];
character = [CCSprite spriteWithSpriteFrame:anim];
character.position = ccp(160, 240);
[self addChild:character];
Thanks in advance,
John
Maybe it was just a cut-and-paste error, but it looks like you're telling the sprite to repeat the animation BEFORE you create it, so the character sprite you are adding to the node never gets the CCAnimate action sent to it.
You're not adding spriteFrames as the addFrame method requires.
with this line:
[character do:repeating];
maybe you're looking for [character runAction:repeating];
character = [CCSprite
spriteWithSpriteFrame:anim];
Here, anim is not a spriteFrame, it's a CCanimation.
basically, you have a few problems.
you could try something like this using zwoptex to create your .plist file:
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
[cache addSpriteFramesWithFile:#"runImages.plist"];
CCSprite *startingImage = [CCSprite spriteWithSpriteFrameName:#"run1.png"];
[self addChild:startingImage];
//create your sprite frames
NSArray *animFrames = [[NSArray alloc] initWithCapacity:6];
[animFrames addFrame:[cache spriteFrameByName:#"run2.png"]];
[animFrames addFrame:[cache spriteFrameByName:#"run1.png"]];
[animFrames addFrame:[cache spriteFrameByName:#"run3.png"]];
[animFrames addFrame:[cache spriteFrameByName:#"run4.png"]];
[animFrames addFrame:[cache spriteFrameByName:#"run3.png"]];
[animFrames addFrame:[cache spriteFrameByName:#"run1.png"]];
//run the animation
CCAnimation *animation = [CCAnimation animationWithName:#"char_walking" delay:1/12.0 frames:animFrames];
id anim = [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO];
[startingImage runAction:anim];