Monday, April 23, 2012

objective-c count

So, after I load the animations sprite frames into the cache in the game I'm making, I count how many frames each animation has. I do this because in the game config there's a value for the duration of each animation, and when I create the CCAnimation, the delay attr is AnimationDuration/FramesAmount.



To count the amount of frames per animation, I open the animations.plist and iterate the entries getting every key, removing the file name, and storing every frame (as key) in a NSMutableDictionary with the amount as an NSNumber that I update through the iteration. The thing is that I don't like how I need to get the NSNumber intValue, do +1, and then alloc another NSNumber and re set the value for the key. Is there any better way of doing this?? Thanks all! (like.. in php OR JS would be dictonary[frame]++)



Here is the code:
NSString *finalPath = [path stringByAppendingPathComponent:@"animations.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];



NSMutableDictionary *conf = [NSMutableDictionary dictionary];
NSDictionary *frames = [plistData valueForKey:@"frames"];

NSNumber *amount;
for( int i=0; i < [frames count]; i++ ) {
NSString *frame = [[frames allKeys] objectAtIndex:i];

NSRange range = [frame rangeOfString:@"/" options:NSBackwardsSearch];
frame = [frame substringToIndex:range.location];

if( [conf objectForKey:frame] == nil )
amount = [[NSNumber alloc] initWithInt:1];
else
amount = [[NSNumber alloc] initWithInt:[[conf objectForKey:frame] intValue]+1];//amount++...

[conf setObject:amount forKey:frame];
}




No comments:

Post a Comment