How to resize CCSprite after changing the texture?

I have code in my iOS project that changes the CCSprite texture via setTexture, for example:

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

However, the sizes of the CCSprite texture before and after the swap are different, and as a result, the Circle.png texture becomes cropped; stuck in the size of the original texture (as the circle grows).

How to resize after replacing a texture?

( Related but not helpful in resolving this issue )

+3
source share
2 answers

Try the following:

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)];
     }
+9
source

You can simply create a sprite using the spriteWithFile constructor. sizes will be set automatically

0
source

All Articles