我的问题是每当我更改精灵的纹理时,新纹理将保留原始精灵纹理的大小。
我有这段代码:
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
时,我没有遇到这个问题我做错了什么?
答案 0 :(得分: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)];
:)
答案 1 :(得分:0)
或:
CCTexture2D* texture=[[CCTexture2D alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sky" ofType:@"png"]] ];
CCSprite *sprTile=[CCSprite spriteWithTexture:texture];
[self addChild:sprTile];
[texture release];