4
votes

I am using Objective-c and cocos2d, and I am getting the strangest error when combining two pieces of seemingly unrelated code.

The suspected code in the init of a layer.

/* sprite setup */

CCSprite *sprite = [ [ [ StaticSprite alloc ] initWithSheetName: @"001-Fighter01" atPosition: ccp( 200, 200 ) withColumn: 0 withRow: 1 ] autorelease ];
[ map.spriteLayer addChild: sprite ];
CCSprite *sprite2 = [ [ [ StaticSprite alloc ] initWithSheetName: @"001-Fighter01" atPosition: ccp( 250, 200 ) withColumn: 0 withRow: 1 ] autorelease ];
[ map.spriteLayer addChild: sprite2 ];
CCSprite *sprite3 = [ [ [ StaticSprite alloc ] initWithSheetName: @"001-Fighter01" atPosition: ccp( 200, 250 ) withColumn: 0 withRow: 1 ] autorelease ];
[ map.spriteLayer addChild: sprite3 ];

/* ui setup */

// dpad
dPadUI = [ DPad node ];
[ self addChild: dPadUI ];

The DPad creation is giving the error CGImageProviderCreate: invalid image provider size: 55 x 36, which causes a crash. The weird part is, if I remove the sprite setup code, the DPad code works fine, and if I remove the DPad code, the sprite code works fine, but somehow when both are present I get the error.

StaticSprite is a fairly simple extension of the CCSprite class:

@implementation StaticSprite

- ( id ) initWithSheetName: ( NSString * ) sheetName atPosition: ( CGPoint ) initPosition withColumn: ( int ) column withRow: ( int ) row {

    // setup frames
    [ [ CCSpriteFrameCache sharedSpriteFrameCache ] addSpriteFramesWithFile: [ NSString stringWithFormat: @"%@.plist", sheetName ] ];

    if ( self = [ super initWithSpriteFrameName:[ NSString stringWithFormat: @"%@-%d,%d.png", sheetName, column, row ] ] ) {

        int halfSize = [ Geometry spriteHalfSizeFromRect: [ self boundingBox ] ];
        self.anchorPoint = ccp( .5, halfSize / [ self boundingBox ].size.height );

        self.position = initPosition;
        self.zOrder = -self.position.y;
    }

    return self;
}

@end

DPad is also a fairly simple extension of a CCSprite:

@implementation DPad

- ( id ) init {
    if ( self = [ super initWithFile: @"dPad.png" ] ) {
        self.anchorPoint = ccp( 18 / [ self boundingBox ].size.width, .5 );
        self.visible = false;
        self.blendFunc = ( ccBlendFunc ) { GL_SRC_ALPHA, GL_ONE };
        self.opacity = 64;
    }
    return self;
}

- ( void ) activateWithDirection: ( double ) angle {
    self.visible = true;
    [ self setRotation: CC_RADIANS_TO_DEGREES( -angle ) ];
}

- ( void ) deactivate {
    self.visible = false;
}

@end

I seem to be able to add as many static sprites with different images with no issue, and the DPad image is pretty small. Why would both of these sections of code work alone, but when in the same app together, they cause the weird error?

I have tried include renaming the DPad init method to initDefault.

EDIT

I stepped through the cocos2d code and found that the error happens right here. After the error, image turns to nil.

UIImage *image = [[UIImage alloc] initWithContentsOfFile:fullpath];

I know it is finding the image, because it prints out the image's size in its error in the log. I cannot seem to find anyone else who has seen this error message on Google.

Currently I am working around this bug by placing the DPad code before all of the Sprite creation code, which seems to stop the bug from occurring, but I would really like to figure out why this is happening. Sporadic bugs make me very nervous until I can figure them out.

2
Have you tried stepping through the cocos2d code that runs before the crash? Might give a hint.Ben Trengrove
@BenTrengrove stepped through the code. The error seems to occur at the line I added to the bottom of the original post. Basically the error occurs when it tries to create a UIImage object.dqhendricks
What is the value of fullpath at that point ? Where does that error line exist ? I don't see it in any of the code files you have presented.prototypical
It's inside the cocos2d source where it creates imagesBen Trengrove
@prototypical The erroring code is inside of cocos2d. The error happens on the line at the end of my post, as I have stated, which is just a simple UIImage creation. I know the full path is correct, because it finds the size of the image (which it outputs in the error in the log), therefore it found the image.dqhendricks

2 Answers

3
votes

Wow, so this is a weird one. Somewhere in my code i had max instead of MAX, and this was causing all types of havoc throughout my code. Three unexplained bugs that I have been lamenting over for the past few days all went away after that one change. For some reason using max in my code was causing everything from this, to making floatValue return nil unexplainedly, to making adding an object to an array cause an unexplained bad access error.

Thanks to all who helped.

1
votes

This is a long shot but I suspect that xcode "thinks" the filename in the error is located someplace else. I'd suggest trying deleting the current pictures and recopying them to your project with a new name that you didn't used yet (testMeNow.jpg) and give it a shot.