sagarkothari
sagarkothari

Reputation: 24810

Replacing image in sprite - cocos2d game development of iPhone

I want to change the sprite image.

Say for example:

mainSprite=[Sprite spriteWithFile:@"redFile.png"];
[self addChild:mainSprite];

Here, Sprite is already added to a layer. I have mainSprite (pointer) which can access it.

If I change

[mainSprite setOpacity:150];

it works perfectly. But here I want to change the sprite image instead of opacity.

But I don't know how.

Upvotes: 9

Views: 13952

Answers (4)

Ali Raza
Ali Raza

Reputation: 613

Replace image in sprite :

CCTexture2D *tex1 = [[CCTextureCache sharedTextureCache] addImage:@"new-image.png"];
[mainSprite setTexture:tex1]; 

Upvotes: 0

P.J.Radadiya
P.J.Radadiya

Reputation: 1541

Replace image in sprite :

[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"image.png"]];

Upvotes: 0

5upr1
5upr1

Reputation: 181

@sagar: In cocos2d 0.99.x I use

[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"sprite.png"]];

It works. The next question is, how can I get back to my previous sprite? Thanks

Upvotes: 13

sagarkothari
sagarkothari

Reputation: 24810

Ok. Damn Simple.

I find it by R & D.

Texture2D *x=[[Texture2D alloc]initWithImage:[UIImage imageNamed:@"box-purple-dark.png"]];
[mainSprite setTexture:x];

Upvotes: 3

Related Questions