Posts Tagged ‘//’

simple cocos2d animation using .png sequence

Posted in code on March 6th, 2012 by admin – Be the first to comment

Right, so I do a lot of animation in my apps, so here is the most simple creation of it

yourfile.h

@private CCSprite *youranimref;
@private CCAnimation *animation;

yourfile.m

-(id) init
{
    if( (self=[super init] )) {
       
        self.isTouchEnabled = YES;
       
        size = [[CCDirector sharedDirector] winSize];
       
        // DECLARE ANIMATION
        animation = [[CCAnimation alloc] initWithName:@"youranimref" delay:1/24.0];
        youranimref = [CCSprite spriteWithFile:@"yourFirstAnimGrfx.png"];
        [animation addFrameWithFilename: @"yourSecondAnimGrfx.png"];
        [animation addFrameWithFilename: @"yourThirdAnimGrfx.png"];
        [animation addFrameWithFilename: @"yourFourthAnimGrfx.png"];

       
        // ADD ANIMATION
        youranimref.position = ccp( 512, 435 );
        [self addChild:youranimref z:5];

    }
    return self;
}


- (void) dealloc
{

// !!!!! VERY IMPORTANT - REMOVE TEXTURES AND RELEASE REFERENCE TO THE ANIMATION

    [[CCTextureCache sharedTextureCache] removeUnusedTextures];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];

    // RELEASE RETAINED OBJECTS
    [animation release];
   
    [super dealloc];
}

- (void) runAnimation
{
   
    // RUN THE ANIMATION
   
    CCAnimate* action = [CCAnimate actionWithAnimation:animation];
    [youranimref runAction:action];

}