I have recently switched from using separate resource files to using a texture atlas. I experimented with replacing a few [CCSprite spriteWithFile] with [CCSprite spriteWithSpriteFrameName]. This works fine except one thing. The CCSprite’s texture dimensions are incorrect. Here is my code :
CGSize screenSize = [[CCDirector sharedDirector]winSize];
CCSprite * leftArrow = [CCSprite spriteWithSpriteFrameName:@"smallLeftArrow.png"];
CGSize arrowSize = [leftArrow texture].contentSizeInPixels;
CCSprite * selectedLA = [CCSprite spriteWithSpriteFrameName:@"smallLeftArrow.png"];
selectedLA.opacity = 100;
CCMenuItem * leftArrowItem = [CCMenuItemSprite itemFromNormalSprite:leftArrow selectedSprite:selectedLA target:[HelloWorldLayer sharedHelloWorldLayer] selector:@selector(doLeft)];
leftArrowItem.position = ccp(- arrowSize.width, arrowSize.height);
CCLOG(@"arrow.width = %f arrow.height %f",arrowSize.width, arrowSize.height);
This is the output on the debugger:
2011-08-29 18:27:04.239 Zero Gravity Combat[454:307] arrow.width = 1024.000000 arrow.height 1024.000000
The size of the entire texture atlas is 1024 X 1024. I am using texture packer to create the texture atlas. Is there a way to fix this or do I need to create a texture manually to determine dimensions?
You obviously want to get the size of the arrow sprite. Thats simple:
leftArrow.contentSize.What you’ve done is getting the size of the used texture, which is of size 1024×1024. The size of the texture is not always the same size as the sprite’s size.