I'm trying to manually update the displaying of frames from an AtlasAnimation using setDisplayFrame, but I get an error when I do so: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'AtlasSprite#setDisplayFrame. Invalid frame'".
Looking at the source I can see that the error is thrown when it tries to get the frame:
NSAssert( frame, @"AtlasSprite#setDisplayFrame. Invalid frame");
Not sure if I'm totally missing something here in how I'm trying to implement - hopefully someone can point out quickly where I'm going wrong?
@interface GameLayer : Layer {
AtlasSprite *spritePlayer;
AtlasAnimation *walkAnimation;
int frameNumber;
int totalFrames;
}
@end
@implementation GameLayer
- (id) init {
self = [super init];
if ( self != nil ) {
frameNumber = 0;
totalFrames = 2;
CGSize spriteSize = CGSizeMake( 35, 40 );
AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"mage-right.png" capacity:50];
[self addChild:mgr];
spritePlayer = [AtlasSprite spriteWithRect:CGRectMake(0, 0, spriteSize.width, spriteSize.height) spriteManager: mgr];
walkAnimation = [AtlasAnimation animationWithName:@"walk-right" delay:0.2f];
for( int i = 0; i < totalFrames; i++ ) {
int x= i % totalFrames;
int y= i / totalFrames;
[walkAnimation addFrameWithRect:CGRectMake( x * spriteSize.width, y * spriteSize.height, spriteSize.height, spriteSize.height ) ];
}
[mgr addChild:spritePlayer];
CGSize s = [[Director sharedDirector] winSize];
spritePlayer.position = ccp( s.width / 2, s.height / 2);
//[spritePlayer setDisplayFrame:@"walk-right" index:0];
// works if you run animation with these 2 lines
//id action = [RepeatForever actionWithAction:[Animate actionWithAnimation:walkAnimation] ];
//[spritePlayer runAction:action];
[self schedule:@selector(onTick:) interval:0.1];
isTouchEnabled = YES;
}
return self;
}
- (void) onTick: (ccTime)dt {
frameNumber = ( frameNumber + 1 ) % totalFrames;
[spritePlayer setDisplayFrame:@"walk-right" index:frameNumber]; // BOMB
}
It will bomb anywhere I try to set the displayFrame. Am I missing something here?
Many thanks in advance!