Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i simply change a sprite's image in cocos2d?

I've tried

[[CCTextureCache sharedTextureCache] addImage: @"still.png"];

But I always end up with a distorted image for some reason. It's most likely because my images are not the same resolution, but for this app, they can't have the same res. How do I change the sprite's image without going through the complicated process of making a spritesheet or an animation or any of that.

like image 929
user663425 Avatar asked Apr 25 '11 20:04

user663425


2 Answers

urSprite = [CCSprite spriteWithFile:@"one.png"];
urSprite.position = ccp(240,160);
[self urSprite z:5 tag:1];

// Changing the image of the same sprite
[urSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"two.png"]];
like image 132
ShinuShajahan Avatar answered Oct 21 '22 01:10

ShinuShajahan


This is the most straight forward way to change the image of a sprite(if you have it loaded trough a spritesheet) this definitely works (I use it all the time in my game). mySprite is the name of the sprite instance:

[mySprite setDisplayFrame:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"sprite1.png"] ];

like image 10
Marin Todorov Avatar answered Oct 21 '22 01:10

Marin Todorov