Change texture and add action to the sprite?

Take this example:

Add a sprite to a layer having some texture. (created using Sprite::create(something.png))

Now, how to change the texture of a sprite but to put a nice fading in effect with it
Note: fading in effect is easy if I am creating a sprite but how to do it for the texture that is now added.

To explain in other words:
I have a sprite. Now, I want to change it texture while applying the fadingIn effect at the same time…
Is it possible? I doubt actually!!

Thanks

SpriteFrameCache::getInstance()->addSpriteFramesWithFile("texture.plist");

auto textureA = SpriteFrameCache::getInstance()->getSpriteFrameByName("textureA.png");
auto textureB = SpriteFrameCache::getInstance()->getSpriteFrameByName("textureB.png");

auto sp = Sprite::create();
sp->setSpriteFrame(textureA);
sp->setSpriteFrame(textureB);

this will may solve your case.

Hi…
this is for just changing the texture… Thats fine… I am asking while you do

after the statement

I want some fadingIn action… Is it possible?

did you mean to change texture with cross fading effect, one to another?

@hzlov
what is cross fading effect… Is it same as fading effect… I think so!!
I think yes to what you are saying :smile:

I suppose it is possible on through 2 different sprites!! Isn’t it?

I meant that cross fading is A is fading in while B is fading out with overlap

Yes! :slight_smile:

No No, when I said

I meant that I supposed that it is possible only through this way…
Actually, its type its “only” instead of “on” in that sentence :slight_smile: sorry…

Also, ya I want this but on the sprite…

Thanks :smile:

You would always need two sprite nodes, as a sprite can only have one texture at a time.

It can be achieved with a node having two child sprite nodes.

No… even if I am removing the first texture completely without any effect and then putting the the second texture with an effect can also work for me…

Then just use the setSpriteFrame method on the sprite node and add the fading effect. But this will not cross-fade your texture.

Ohk, I see setSpriteFrame… I’ll try this…
but what how to set thsi fading effect? I mean setSpriteFrame will just change the texture and there is not argument for any effect in that…
Are you talking about…
auto spriteK = Sprite::create(“1.png”);
spriteK-> setPosition(Vec2(100,100));
this->addChild(spriteK);
//Now…
auto myAction = FadeIn(0.5);
spriteK-> setSpriteFrame(“2.png”);
spriteK->runAction(myAction);

So, is this the way you’re suggesting?
Also, what is the difference between setting a texture and setting a frame :smile:

Thanks

Exactly.

Yes.

Setting a texture sets the total pixel information stored in a texture.

Setting a frame only sets the information used in the rectangle defined in the sprite, but still storing the complete texture information… The rectangle/frame is able to set/render a small part of the texture.