2014-12-29 33 views
2

我想做一個Spritebuilder iPhone遊戲,我的主角人物_player是一個CCNode,他的孩子是一個CCBFile,它持有角色的空閒動畫。Spritebuilder如何以編程方式更改CCNode的動畫(CCB文件)?

我想將此_player的CCBFile更改爲另一個名爲ForwardDash.ccbi的CCBFile,其中包含玩家觸摸屏幕時的「攻擊」動畫。

這就是我想:

//_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]; 

它失敗,給我的錯誤:「線程1:信號SIGABRT」

+0

這是因爲animar是CCNode(或子類)實例,而不是CCSpriteFrame – LearnCocos2D

回答

1

setSpriteFrame是不是你正在尋找的方法。如果你想保持你當前建行的設置,你應該能夠完成你想要做:

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

雖然這工作,我建議你嘗試採取在SpriteBuilder不同的動畫時間線的優勢。您可以將新時間線添加到CCB文件,然後以編程方式更改動畫,而無需刪除和添加新節點。您可以閱讀更多關於使用時間表here的信息。一旦建立,你就可以開始一個新的動畫:

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