0

I am trying to play through an animation using these files that I created with texture packer (footballAnim-hd.pvr.ccz) and (footballAnim.pv-hd.plist) but I have encountered a problem. Here is my code:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"footballAnim.pv.plist"];

    CCSprite *football = [CCSprite spriteWithSpriteFrameName:@"Football59-hd.png"];
    football.position = ccp(100, 100);

    CCSprite *football2 = [CCSprite spriteWithSpriteFrameName:@"Football59-hd.png"];
    football2.position = ccp(120, 100);

    //This is the animation
    id anim = [CCAnimate actionWithSpriteSequence:@"Football%d-hd.png"
                                                  numFrames:59
                                                      delay:0.01
                                       restoreOriginalFrame:NO];
    //This is the animation
    id anim2 = [CCAnimate actionWithSpriteSequence:@"Football%d-hd.png"
                                        numFrames:59
                                            delay:0.01
                             restoreOriginalFrame:NO];

    //This is the action
    id repeat = [CCRepeatForever actionWithAction:anim];

    //This is the action
    id repeat2 = [CCRepeatForever actionWithAction:anim2];

     CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:@"footballAnim.pvr.ccz"];
    [self addChild:batchNode];
    [batchNode addChild:football];
    [batchNode addChild:football2];

    [football runAction:repeat];
    [football2 runAction:repeat2];

So My problem is I am using kobold2d which is using (cocos2d v1.1.0-beta2b) and when I try to play this animation it only plays through half of the frames, but then I tried this (EXACT) code in another cocos2d project which is using (cocos2d v1.0.0-rc) and it works like a charm. Is this a bug in cocos2d or am I not doing something right?

Stephen
  • 499
  • 9
  • 28

2 Answers2

1

Indeed cocos2d 1.1 changed how animation delays work, and it was a breaking change either intentionally or caused by a bug (I really couldn't tell). The change is so weird that after a couple minutes looking into this, I gave up and reverted my own project back to 1.0.1. This animation issue is also why I keep the download link for Kobold2D v1.0.5 live.

Maybe this animation change has some use, I read the forum post but even how it's supposed to work is not really making sense to me. Or maybe it does and it's just badly explained, or the implementation simply has a bug or two. I haven't had these animation issues in cocos2d 2.0 though. Maybe it was fixed there, or maybe the change was never applied to cocos2d 2.0.

FWIW, cocos2d-iphone v1.x doesn't seem to be updated anymore. The last official commit to the v1.x master branch was 6 months ago, the last commit to the v1.x develop branch 4 months ago. It's time to jump ships.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
0

I figured out how to fix my problem. Someone on the cocos2d forum suggested changing the update method in CCActionInterval.m (from this)

-(void) update: (ccTime) t
{
    NSArray *frames = [animation_ frames];
    NSUInteger numberOfFrames = [frames count];
    CCSpriteFrame *frameToDisplay = nil;

    for( NSUInteger i=nextFrame_; i < numberOfFrames; i++ ) {
        NSNumber *splitTime = [splitTimes_ objectAtIndex:i];

        if( [splitTime floatValue] <= t ) {
            CCAnimationFrame *frame = [frames objectAtIndex:i];
            frameToDisplay = [frame spriteFrame];
            [(CCSprite*)target_ setDisplayFrame: frameToDisplay];

            NSDictionary *dict = [frame userInfo];
            if( dict )
                [[NSNotificationCenter defaultCenter] postNotificationName:CCAnimationFrameDisplayedNotification object:target_ userInfo:dict];

            nextFrame_ = i+1;

            break;
        }
    }
}

To this

-(void) update: (ccTime) t
{
    NSArray *frames = [animation_ frames];
    NSUInteger numberOfFrames = [frames count];

    NSUInteger idx = t * numberOfFrames;

    if( idx >= numberOfFrames )
        idx = numberOfFrames -1;

    CCAnimationFrame *frame = [frames objectAtIndex:idx];
    CCSpriteFrame *frameToDisplay = [frame spriteFrame];
    [(CCSprite*)target_ setDisplayFrame: frameToDisplay];

    NSDictionary *dict = [frame userInfo];
    if( dict )
        [[NSNotificationCenter defaultCenter] postNotificationName:CCAnimationFrameDisplayedNotification object:target_ userInfo:dict];
}

This worked for me, but I don't know if it is the ideal fix.

Stephen
  • 499
  • 9
  • 28