iphonesdkcocos2d-iphone

cut a sprite sheet on cocos2d for animation


I want to cut a sprite in 6 equivalent parts just with one image, a .png file which I found on the web, no with texturepacker, (the image below by example)

I can take other way, but I want to know if I can do that. any one haves idea?

enter image description here


Solution

  • I'll revise my answer if this isn't what you were asking about, but I think you are asking how to 'manually run an animation' using a spritesheet without a plist. Here's one way. It would be better if you encapsulated this into it's own class, but this can push you in the right direction I think:

    ManualAnimationTest.h

    @interface ManualAnimationTest : CCLayer
    {
        CCSprite *animatedSprite;
        int x,y;
        float animatedSpriteWidth, animatedSpriteHeight;
        int animatedSpriteColumns, animatedSpriteRows;
    }
    @end
    

    ManualAnimationTest.m

    #import "ManualAnimationTest.h"
    
    @implementation ManualAnimationTest
    -(id) init
    {
        if( (self=[super init]))
        {
            CGSize s = [CCDirector sharedDirector].winSize;
            x = 0;
            y = 0;
    
            animatedSpriteColumns = 3;
            animatedSpriteRows = 2;
            animatedSpriteWidth = 95.0f;
            animatedSpriteHeight = 125.0f;
    
            animatedSprite = [CCSprite spriteWithFile:@"animal_animation.png" rect:CGRectMake(x * animatedSpriteWidth,y * animatedSpriteHeight,animatedSpriteWidth,animatedSpriteHeight)];
    
            [self addChild:animatedSprite];
            [animatedSprite setPosition:ccp(s.width / 2.0f, s.height / 2.0f)];
            [self schedule:@selector(animateAnimatedSprite) interval:0.5f];
        }
        return self;
    }
    
    -(void) animateAnimatedSprite
    {
        [animatedSprite setTextureRect:CGRectMake(x * animatedSpriteWidth, y * animatedSpriteHeight, animatedSpriteWidth, animatedSpriteHeight)];
        x +=1;
        if(x > (animatedSpriteColumns - 1))
        {
            x = 0;
            y +=1;
        }
    
        if(y > (animatedSpriteRows - 1))
        {
            y = 0;
        }
    }
    
    
    @end