我正在寻找从精灵表做一个基本的动画。我从在线学习的教程中获得了大部分格式/代码,但是它只做了一个定向运动。基本上,我希望有 4 种不同的动画,使我的精灵使用适当的动画在触摸的方向上“行走”。我看过很多帖子和教程,但似乎都没有使这个概念更加清晰,也没有指出我的错误。
我有 4 组 4 个动画。每个基本方向的集合(标记为上、下、左、右)。
我的精灵表是这样设置的:
注意:还有一个 *plist 文件是根据这些单独的文件自动生成的
[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
在我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