Search code examples
iosanimationcocos2d-iphonespritebuilder

Spritebuilder how can I change the animation ( CCB file ) of a CCNode programmatically?


I'm trying to make a Spritebuilder iPhone game, and my main character _player is a CCNode whose child is a CCBFile which holds the character's idle animation.

I want to change this _player's CCBFile to another CCBFile called ForwardDash.ccbi which contains the 'attack' animation when the player touches the screen.

This is what I'm trying:

 //_player is a CCNode, its first child is the CCBFile with the idle animation.
 //animar is a pointer to the CCBFile with the ForwardDash animation

 CCSprite *wat = _player.children[0];
           CCNode *animar = [CCBReader load:@"ForwardDash"];
           [wat setSpriteFrame: (CCSpriteFrame*)animar];

It fails and gives me the error: 'Thread 1: signal SIGABRT'


Solution

  • setSpriteFrame is not the method you are looking. If you want to keep your current CCB setup you should be able to accomplish what you want by doing:

    CCSprite *wat = _player.children[0];
    [wat removeFromParent];
    CCNode *animar = [CCBReader load:@"ForwardDash"];
    [_player addChild:animar];
    

    While this works, I would recommend you try taking advantage of different animation timelines in SpriteBuilder. You can add new timelines to a CCB file and then change animations programmatically without removing and adding a new node. You can read a bit more about using timelines here. Once set up, you'll be able to start a new animation with:

    CCSprite *wat = _player.children[0];
    CCAnimationManager* animationManager = wat.animationManager;
    [animationManager runAnimationsForSequenceNamed:@"ForwardDash"];