Search code examples
cocos2d-iphonekobold2d

Kobold2d animation problems


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?


Solution

  • 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.