Raven Dreamer
Raven Dreamer

Reputation: 7160

How do I change a CCSprite's Dimensions after changing texture?

I have a piece of code in my iOS project that swaps the texture of a CCSprite via setTexture, like so:

[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"Circle.png"]];

However, the dimensions of the CCSprite's texture before and after the swap are different, and as a result, the Circle.png texture is getting cropped; stuck at the size of original texture (as the circle is larger).

How can I adjust the size after swapping the Texture?

(Related, but not helpful in solving this)

Upvotes: 3

Views: 3382

Answers (2)

Saikat
Saikat

Reputation: 2633

Try this:

CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:@"Circle.png"];

    if (texture)
    {
                // Get the size of the new texture:
        CGSize size = [texture contentSize];

        [sprite setTexture:texture];
                // use the size of the new texture:
        [sprite setTextureRect:CGRectMake(0.0f, 0.0f, size.width,size.height)];
     }

Upvotes: 9

Morion
Morion

Reputation: 10860

You can just recreate sprite with spriteWithFile constructor. the dimensions will be set automatically

Upvotes: 0

Related Questions