Monday, 27 October 2014

Replace the sprite in Cocos2d

Change the sprite in run time in Cocos2d:



CCSprite *sprite = [CCSprite spriteWithFile:@"image1.jpg"];
sprite.position = ccp( size.width /2 , size.height/2 );

[self addChild:sprite];


Run time with same image size(widthXheight):


CCTexture2D* newtexture = [[CCTextureCache sharedTextureCache] addImage:@"backGround5.jpg"];
[sprite setTexture: newtexture];


Run time with different image size(widthXheight):

CCTexture2D* newtexture = [[CCTextureCache sharedTextureCache] addImage:@"backGround5.jpg"];
[sprite setTexture: newtexture];
[sprite setTextureRect:CGRectMake(0, 0, 568, 320)];

Note: If the size is different just you have to change the width and height, x,y always constant.

No comments:

Post a Comment