0
votes

I am looking to do a basic animation from a spritesheet. I got most my formatting/code from a tutorial I was following online, however it only did one directional motion. Basically I am looking to have 4 different animations that cause my sprite to "walk" in the direction of the touch with the appropriate animation. I've looked at many posts and tutorials but none of them seem to make the concept any more clear nor point out my errors.

I have 4 sets of 4 animations. A set for each cardinal direction (labeled Up, Down, Left, Right).

My sprite sheet is set up like so:

Note: There is also a *plist file that was automatically generated based on these individual files

[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

This code that attempts to make these Actions in my init function in the HelloWorldLayer:

Note: This block of code is where it crashes!! I am not entirely sure where in here it does, but the error info is below as well!

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

Error Info:

*** 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
1

1 Answers

0
votes

Well, this was silly of me. Luckily I was doing the SpriteSheet manipulation correctly.

The following line needed remove:

     [self.player runAction:self.walkAction];

I don't initialize the CCAction walkAction until later when I receive a touch on the screen, so this line was crashing because self.walkAction was nil by default.