views:

388

answers:

2

My problem is that whenever I change the texture of a sprite, the new texture will retain the size of the original sprite's texture.

I have this code:

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];

The new sprite is stretched or compressed depending on the size of the original sprite. If the original sprite is larger, the new texture is stretched.

I didn't have this problem when I was using cocos2d v0.8

What am I doing wrong?

A: 

Solved:

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)];

:)

marie
A: 

GOOD :) I was looking to an answers for this problem for ages!!! It's a very common problem..