спрайт лист анимации - PullRequest
       11

спрайт лист анимации

0 голосов
/ 15 февраля 2012

Я разрабатываю приложение, используя cocos2d и box2d phisycs.Я хочу оживить мой спрайт при движении.Я сделал файлы * .plist и * .png в Zwoptex и добавил их в свой проект.Сейчас я пытаюсь создать спрайт:

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

, но этот код создает один спрайт со всеми кадрами на узле.Что я делаю не так?

1 Ответ

2 голосов
/ 15 февраля 2012

Извлечь кадр, как это ...

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