Updating a Sprite, in place; Things I didn’t know about Cocos2d-x, but I probably could have read in the docs somewhere #8

You can update a Sprite’s SpriteFrame, instead of only its texture. This is important because if you are using a SpriteFrame to batch load sprites and then try to swap one sprite out for another using setTexture, nothing will happen because its the same texture, only the textureRect is any different. So your first instinct is to manually force the new textureRect:

old_sprite->setTextureRect(new_sprite->getTextureRect(), new_sprite->isTextureRectRotated(), new_sprite->getTextureRect().size);

but really, all you need to do is reuse the sprite frame:

old_sprite->setSpriteFrame(new_sprite->getSpriteFrame());

Much easier.

Leave a Reply

Your email address will not be published.