I'm trying to animate a CCSprite in cocos2d v3 following this question, but I'm getting the following error message in Log:

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM
 insertObject:atIndex:]: object cannot be nil'

This is my code:

NSMutableArray *animationFrames = [NSMutableArray array];
    int frameCount = 0;
    for(int i = 1; i <= 9; ++i)
        CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"hero-%d.png", i]];

            [animationFrames addObject:spriteFrame]; //ERROR OCCUR THIS LINE

        if (frameCount == 8) break;

The problem is that all spriteFrames are nil, but I don't why. I already tested the i values with NSLog, and also tried to add hero-1.png to a CCSprite object and works fine. I also tried to add all fixed hero-1.png in all frames to verify if the problem was with i variable. But didn't work also.


This work! in cocos2d 3.x

NSMutableArray *frames = [NSMutableArray array];

int frameCount = 0;

    for (int i = 1; i <= 5; i++) {           
        CCSpriteFrame *frame = [CCSpriteFrame frameWithImageNamed:[NSString stringWithFormat:@"dice0%d.png",i]];

        [frames addObject:frame];

CCSprite *sprite = [CCSprite spriteWithSpriteFrame:frames.firstObject];
[self addChild:sprite]; // or to whatever object it will be parented to
sprite.position = ccp(WIDTH*0.5, HEIGHT*0.5);

CCAnimation *animation = [CCAnimation animationWithSpriteFrames:frames delay:0.5];
CCAction *animateAction = [CCActionAnimate actionWithAnimation:animation];
[sprite runAction:animateAction];
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.