0
votes

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?

1

1 Answers

2
votes

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]];
         }