2013-05-18 40 views
0

我正在尋找從spritesheet做基本的動畫。我從網上學習的教程中獲得了大部分格式/代碼,但它只做了一個定向運動。基本上我正在尋找有4種不同的動畫,導致我的精靈在適當的動畫中沿着觸摸的方向「走動」。我看過很多帖子和教程,但他們都沒有讓這個概念更清晰,也沒有指出我的錯誤。我有4組4動畫。每個基本方向的一組(標記爲向上,向下,向左,向右)。Cocos2D:從spritesheet的基本動畫

我的精靈表設置像這樣:

注:還有自動生成基於這些單獨的文件

[player1.png] [player2.png] [player3.png] [player4.png] //Walking down animations 

[player5.png] [player6.png] [player7.png] [player8.png] //Walking left animations 

[player9.png] [player10.png] [player11.png] [player12.png] //Walking right animations 

[player13.png] [player14.png] [player15.png] [player16.png] //Walking up animations 

此代碼試圖使這些Actions一個* plist文件在我的init功能在HelloWorldLayer:

注意:這塊代碼是它崩潰的地方!我不完全確定它在哪裏,但錯誤信息也在下面!

//Store the sprite info in cahce 
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"player.plist"]; 

//Load the spritesheet into a BatchNode 
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"player.png"]; 
[self addChild:spriteSheet]; 

//Create frame arrays and store the appropriate .png sets 
NSMutableArray *walkPlayerRightFrames = [NSMutableArray array]; 
NSMutableArray *walkPlayerLeftFrames = [NSMutableArray array]; 
NSMutableArray *walkPlayerUpFrames = [NSMutableArray array]; 
NSMutableArray *walkPlayerDownFrames = [NSMutableArray array]; 

for (int i=1; i<=4; i++) 
{ 
    [walkPlayerDownFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"player%d.png", i]]]; 

    [walkPlayerLeftFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"player%d.png", i+4]]]; 

    [walkPlayerRightFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"player%d.png", i+8]]]; 

    [walkPlayerUpFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"player%d.png", i+12]]]; 
} 


//These are of type "CCAnimation" and are declared in the header with appropriate @property stuff 
_walkPlayerUp = [CCAnimation 
          animationWithSpriteFrames:walkPlayerUpFrames delay:0.1f]; 

_walkPlayerRight = [CCAnimation 
          animationWithSpriteFrames:walkPlayerRightFrames delay:0.1f]; 

_walkPlayerLeft = [CCAnimation 
          animationWithSpriteFrames:walkPlayerLeftFrames delay:0.1f]; 

_walkPlayerDown = [CCAnimation 
          animationWithSpriteFrames:walkPlayerDownFrames delay:0.1f]; 

//Declare a starting image and starting position   
self.player = [CCSprite spriteWithSpriteFrameName:@"player1.png"]; 
self.player.position = ccp(100, 100); 

//Set an action for when the player runs   
[self.player runAction:self.walkAction]; 

//Add player to the spriteSheet 
[spriteSheet addChild:self.player]; 

self.touchEnabled = YES; 

錯誤信息:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Argument must be non-nil' 
*** First throw call stack: 
(0x250d012 0x1eeae7e 0x250ce78 0x160b665 0x5c2a0 0xdf42f 0x59495 0xde9e2 0xe16f1 0x1efe663 0xbfa29 0xbe07a 0x5bfb4 0x32272 0xb44b8 0x15b4fb4 0x15ecb1a 0xb4c4c 0xddbaf 0xb5392 0xb8829 0xb0a2dd 0x1efe6b0 0x3cafc0 0x3bf33c 0x3caeaf 0xba92bd 0xaf1b56 0xaf066f 0xaf0589 0xaef7e4 0xaef61e 0xaf03d9 0xaf32d2 0xb9d99c 0xaea574 0xaea76f 0xaea905 0xaf3917 0xde183 0xab7157 0xab7747 0xab894b 0xac9cb5 0xacabeb 0xabc698 0x3215df9 0x3215ad0 0x2482bf5 0x2482962 0x24b3bb6 0x24b2f44 0x24b2e1b 0xab817a 0xab9ffc 0xdd9b6 0x1f35 0x1) 
libc++abi.dylib: terminate called throwing an exception 

回答

0

好了,這是我傻的。幸運的是我正在正確地執行SpriteSheet操作。

下面這行需要進行刪除:

 [self.player runAction:self.walkAction]; 

我不初始化CCAction walkAction直到後來,當我收到了屏幕上的觸摸,所以這條線被撞毀,因爲self.walkActionnil默認。