0
votes

Hello I am making a cocos2d side scroller. I am using a plist file to supply my game data. For some reason the code is not reading the plist file and when I want to display an image, and the image is in the plist file, then I get a SIGABRT error.

Here is the plist file:enter image description here

This is the code that is causing the error:

+(id)createRedEnemyWithDictionary:(NSDictionary *)redEnemyDictionary{
return [[[self alloc]initWithDictionary:(NSDictionary*)redEnemyDictionary]autorelease];
}

-(id)initWithDictionary:(NSDictionary*)redEnemyDictionary{
if ((self = [super init])) {
    //Make the redEnemy
    redEnemySprite = [CCSprite spriteWithFile:[redEnemyDictionary    objectForKey:@"RedEnemyBaseImage"]];
    [self addChild:redEnemySprite];

This is the error I am getting:

*** Assertion failure in -[CCSprite initWithFile:]

This is the line of code my exception breakpoint points to:

NSAssert(filename != nil, @"Invalid filename for sprite");
1
put some breakpoints in your code and verify that content of dictionary you are passing to your init method is what you expect. - Kreiri
I want to have the name of the red enemy sprite picture file which is stored in the key of the dictionary called RedEnemyBaseImage but it said that there is no file matching that file name in my resource file. I checked and i do have a picture of the same name. This is also a problem for the main character of my app i am trying to do this by the same method i am using for the red enemy with the dictionary and plist file - PoKoBros

1 Answers

0
votes

Make your live easier by optimizing code for readability (that means "logability" too). Instead of this:

redEnemySprite = [CCSprite spriteWithFile:
    [redEnemyDictionary objectForKey:@"RedEnemyBaseImage"]];

Rewrite as follows:

NSString* img = [redEnemyDictionary objectForKey:@"RedEnemyBaseImage"];
NSLog(@"using image file: %@", img);
redEnemySprite = [CCSprite spriteWithFile:img];

The extra temporary variable img allows you to inspect the contents of the img variable when you set a debug breakpoint, and coincidentally you can also log it.

If you this logs nil, inspect the dictionary by logging it:

NSLog(@"dict: %@", redEnemyDictionary);

A common mistake in nested dictionary structures is to forget to index a subpath within the dictionary. Ie the plist above is a dictionary that contains a key "Characters" which is another dictionary whose "RedEnemy" key contains another dictionary with strings. If you forget only one of the keys or make a typo, the returned object will be nil and so will all subsequent accesses to the "nil object" dictionary.

This stuff is easy to debug if you know how to set breakpoints, single-step through code and inspect variables in the debugger. If you aren't intimate with these processes you should read the Xcode debugging guide.