如何在更改纹理后更改CCSprite的尺寸?

Rav*_*mer 3 cocos2d-iphone

我的iOS项目中有一段代码通过setTexture交换CCSprite的纹理,如下所示:

[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"Circle.png"]];
Run Code Online (Sandbox Code Playgroud)

但是,交换前后CCSprite纹理的尺寸不同,因此,Circle.png纹理会被裁剪掉; 卡在原始纹理的大小(因为圆圈较大).

交换纹理后如何调整大小?

(相关,但没有帮助解决这个问题)

Sai*_*kat 9

试试这个:

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)];
     }
Run Code Online (Sandbox Code Playgroud)