1

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!

4

1 回答 1

0

Sprite 有一个setTexture在 cocos2dx 3.0 中调用的成员函数。如果您在场景中保留一个指向精灵对象的成员指针,则可以将函数更新为以下内容:

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);
        }
    }
于 2016-04-03T19:30:30.967 回答