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.