2010-04-27 24 views
2

我的問題是,每當我改變精靈的質感,新的紋理會保留原來的精靈的紋理大小。問題與的Texture2D遷移到CCTexture2D(設置CCSprite紋理)

我有這樣的代碼:

mySprite = [CCSprite spriteWithFile:@"mySprite.png"]]; 

... 

// change current stage here 

CCTexture2D *newTexture=[[[CCTexture2D alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"stage number %d.png",currentStageNumber]]]autorelease]; 

[mySprite setTexture:newTexture]; 

新精靈取決於原始精靈的尺寸被拉伸或壓縮。 如果原始精靈較大,新的紋理被拉伸。

當我使用cocos2d的V0.8

我在做什麼錯了,我沒有這個問題?

回答

8

解決:

mySprite = [CCSprite spriteWithFile:@"mySprite.png"]]; 

... 

// change current stage here 

CCTexture2D *newTexture=[[[CCTexture2D alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"stage number %d.png",currentStageNumber]]]autorelease]; 

[mySprite setTexture:newTexture]; 

[mySprite setTextureRect:CGRectMake(0,0, newTexture.contentSize.width, newTexture.contentSize.height)]; 

:)

+0

的'setTextureRect'調用是一個神奇的東西,它使這項工作。沒有它,你的新的紋理可以,如果你的精靈應用了一個自定義的比例因子被可怕的扭曲。 – aroth 2011-08-26 00:41:10

0

或:

CCTexture2D* texture=[[CCTexture2D alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sky" ofType:@"png"]] ]; 
    CCSprite *sprTile=[CCSprite spriteWithTexture:texture]; 

    [self addChild:sprTile]; 
    [texture release];