2012-04-18 68 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