Problem with transferring from Texture2D to CCTexture2D (CCSprite texture setting)

My problem is that whenever I change the texture of the sprite, the new texture will keep the size of the original texture of the sprite.

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 shrunk based 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?

+2


a source to share


2 answers


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

      



:)

+8


a source


or



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

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

      

0


a source







All Articles