user1050548
user1050548

Reputation: 381

Cocos2d contentSize for sprite not working

I'm using cocos2d. After i load the ccspriteframecache and ccspritebatchnode, i made a ccsprite. But i need the contentsize of the sprite stage.

However it returns 480.000000, 320.000000 instead, which the size of the whole screen. My image is a lot smaller.

[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"nBack.plist"];
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"nBack.png"]

CCSprite *stage = [CCSprite spriteWithSpriteFrameName:@"nBack0001.png"];

stage.position = ccp( winSize.width /2, winSize.height/2 );
CGSize stageSize = stage.contentSize;
NSLog( @"%f,%f", stageSize.width, stageSize.height );
[self addChild:stage z:1];

Upvotes: 1

Views: 1806

Answers (2)

CodeSmile
CodeSmile

Reputation: 64477

The contentSize is correct. You may want to double-check the dimensions of your image.

I can imagine that you drew the image on a 480x320 transparent background. That would leave the image size at 480x320 but the tools used to create the texture atlas will (temporarily) strip the extraneous transparent area. But it will still be used in the rendering of the image on the iPhone and affect your image's contentSize.

Upvotes: 1

Haroon
Haroon

Reputation: 695

I think you can get the contentSize by using the boundingBox.

in your case:

CGSize stageSize = [stage boundingBox];

Try this hopefully you will get desired result but if you scale the sprite you will get the content size according to that.

As :

stage.scale = 0.5f; or stage.scale = 2.0f;`

you will find that in first case the size is reduced to half and in second case the size is double. boundingBox gives the current content size of the node in play.

I think this will help you in getting the size.

Thanks

Upvotes: 0

Related Questions