I am using this https://github.com/qiankanglai/ImagePicker ImagePicker utility I am using this code to set sprite texture:
void HelloWorldScene::didFinishPickingWithResult(cocos2d::Texture2D* result)
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
if(result == nullptr){
return;
}
// sprite->removeFromParentAndCleanup(true);
ClippingNode * clipper = ClippingNode::create();
clipper->setPosition(visibleSize.width / 2, visibleSize.height / 2);
clipper->setTag( kTagClipperNode );
this->addChild(clipper);
DrawNode * stencil = DrawNode::create();
stencil->drawSolidCircle(Vec2(clipper->getBoundingBox().size.width / 2, clipper->getBoundingBox().size.height / 2), 100, 0, 200, Color4F::MAGENTA);
clipper->setStencil(stencil);
clipper->setInverted(false);
auto sprite = cocos2d::Sprite::createWithTexture(result);
sprite->setPosition( Vec2(clipper->getContentSize().width / 2, clipper->getContentSize().height / 2));
clipper->addChild(sprite);
this->addChild(clipper);
}
actually i am getting the Texture 2D object from gallery and setting it on the sprite. This code works great but if I want to replace the sprite texture then the same code is executed again and a new clipping node object is added and also a new sprite over the previous one...
I want to know how can I solve this issue? I want to replace the old sprite texture with new prite texture when selects a new photo from the gallery.
Thanks in advance!
Sprite has a member function called setTexture
in cocos2dx 3.0. If you keep a have a member pointer to the sprite object in your scene, you can update your function to be the following:
void HelloWorldScene::didFinishPickingWithResult(cocos2d::Texture2D* result)
{
if(result == nullptr){
return;
}
if(m_sprite)
{
m_sprite->setTexture(result);
}
else
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
ClippingNode * clipper = ClippingNode::create();
clipper->setPosition(visibleSize.width / 2, visibleSize.height / 2);
clipper->setTag( kTagClipperNode );
this->addChild(clipper);
DrawNode * stencil = DrawNode::create();
stencil->drawSolidCircle(Vec2(clipper->getBoundingBox().size.width / 2, clipper->getBoundingBox().size.height / 2), 100, 0, 200, Color4F::MAGENTA);
clipper->setStencil(stencil);
clipper->setInverted(false);
m_sprite = cocos2d::Sprite::createWithTexture(result);
m_sprite->setPosition( Vec2(clipper->getContentSize().width / 2,clipper->getContentSize().height / 2));
clipper->addChild(m_sprite);
this->addChild(clipper);
}
}