[SOLVED] How to replace a sprite texture?

[SOLVED] How to replace a sprite texture?
0.0 0

#1

Hi,

I am trying to change the sprite looks when a sprite has been selected. I have no luck so far changing the sprite look using this code:

void Object::replaceSprite(const string & resourceName)
{
	cocos2d::SpriteFrameCache * spriteFrameCache = cocos2d::SpriteFrameCache::getInstance();
	cocos2d::SpriteFrame * spriteFrame = spriteFrameCache->getSpriteFrameByName(resourceName);

	mSprite->setTexture(spriteFrame->getTexture());
	//mSprite->setDisplayFrame(spriteFrame);
}

What is the proper way of doing this? I am loading my sprite through a spritesheet and I prefer my sprite to be loaded that way, nothing more.

Thanks for your help.


#2

mSprite->setSpriteFrame(“imagename.png”);


#3
mSprite->setSpriteFrame("imagename.png");

Oh Thanks i_d_k. But unfortunately it didn’t work.


#4

Can somebody actually help? I thought this community was active. Well, I guess not. Can anyone at least show how you can replace a sprite’s image/texture from a spritesheet file? Did I miss anything?


#5

Also, checking the debugger the variable _textureFilename is empty. Is this suppose to be like this? It seems its not catching the resource name. I don’t know if the spriteFrame able to load the sprite correctly. Any ideas how to get this done?

Is there anyway I could check if the spriteFrame able to load the correct texture data?

Thanks a lot.


#6

what do you mean when you are saying that sprite is selected ?
Are you saying that when the sprite is touched, you need to change the texture…??


#7

You should be able to set the sprite frame you received from the cache. spriteFrame will be null if it is not found. If not found there’s an issue with how you’re adding sprite frames from plist.

mSprite->setSpriteFrame(spriteFrame);

#8

what do you mean when you are saying that sprite is selected ?
Are you saying that when the sprite is touched, you need to change the texture…??

You know what. It doesn’t matter what I am trying to achieve really. Why does it matter? Gonna suggest another way to do it? That this is better than this than that, blah blah. I don’t need it to quite honest. I am asking a question. I just want to know how to replace the sprite texture dynamically. If it ever possible at all.

The good news is,all of the previous suggestions and my attempts work. What’s weird is that, the new texture is on the bottom of the old one, I checked the documentation and found a method under Sprite class named as setGlobalZOrder():

void Object::replaceSprite(const string & resourceName , int zIndex)
{
	cocos2d::SpriteFrameCache * spriteFrameCache = cocos2d::SpriteFrameCache::sharedSpriteFrameCache();
	cocos2d::SpriteFrame * spriteFrame = spriteFrameCache->getSpriteFrameByName(resourceName);

	mSprite->setSpriteFrame(spriteFrame);
	mSprite->setGlobalZOrder(zIndex);
}

This currently work on my cocos2d-x exercise. I don’t know how will the setGlobalZOrder will scale with other sprite on but that is another discussion. I am going to read more about that function and its implementation later this evening. So that’s how I done it. I hope this will help someone in the future, just in case.

Thanks everyone for the help!


#9

Well FYI,
yes, obviously it is important for other developers to know what you are trying to do to get a clear idea for solving your problem.

Happy that you found a way.
But, I would suggest you to learn some polite way to address when you are in this forum. :smile:
Ego is knowledge killer my friend.

Happy Coding :smile:


#10

what is resourceName?


#11

It’s always good to explain what your trying to achieve. From my experience asking for help from more experienced developers, by the way. Also no need to be negative, remember your here asking for help. Be respectful and thankful. Us noobs rely heavily on the free help of experienced developers, its magic


#12

For me breaks here ?

Any Idea Why ?


#13

What is your “spriteFrame” name assigned as? Could you list the error your getting.


#14

You may load your sprite into frame cache first
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(your_plist)