2012-04-18 105 views
3

我在我的iOS项目中的一段代码,通过的SetTexture交换一个CCSprite的质感,就像这样:如何在更改纹理后更改CCSprite的尺寸?

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

然而,CCSprite的质感的前后互换后的尺寸是不同的,如结果,Circle.png纹理正在裁剪;坚持原始纹理的大小(因为圆圈较大)。

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

Related, but not helpful in solving this

回答

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

我不得不添加更多的代码来获得适当的长度来实现这个功能,所以我将它添加为建议的编辑。 – 2012-04-23 15:23:32

0

您只需重新创建spriteWithFile构造精灵。尺寸会自动设置为

+0

这样做运行到内存管理,坏的访问,和僵尸的问题。你能详细说明一下你如何进行内存管理吗? – 2012-04-23 15:09:11

+0

这里我没有看到任何内存问题。只需从父项中删除第一个精灵,用新文件创建新精灵,并将其添加到所需的父项。 – Morion 2012-04-23 16:34:19