如何在 cocos2dx C++ v3.10 中更改 Sprite 纹理?
How to change Sprite texture in cocos2dx C++ v3.10?
我正在使用这个 https://github.com/qiankanglai/ImagePicker ImagePicker 实用程序我正在使用这个代码来设置精灵纹理:
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);
}
实际上我正在从图库中获取 Texture 2D 对象并将其设置在 sprite 上。
这段代码效果很好,但是如果我想替换精灵纹理,则再次执行相同的代码并添加一个新的剪辑节点对象以及一个新的精灵在前一个上...
我想知道如何解决这个问题?从图库中选择新照片时,我想用新的 prite 纹理替换旧的 sprite 纹理。
提前致谢!
Sprite在cocos2dx 3.0中有一个成员函数叫setTexture
。如果您在场景中保留一个指向精灵对象的成员指针,则可以将您的函数更新为以下内容:
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);
}
}
我正在使用这个 https://github.com/qiankanglai/ImagePicker ImagePicker 实用程序我正在使用这个代码来设置精灵纹理:
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);
}
实际上我正在从图库中获取 Texture 2D 对象并将其设置在 sprite 上。 这段代码效果很好,但是如果我想替换精灵纹理,则再次执行相同的代码并添加一个新的剪辑节点对象以及一个新的精灵在前一个上...
我想知道如何解决这个问题?从图库中选择新照片时,我想用新的 prite 纹理替换旧的 sprite 纹理。
提前致谢!
Sprite在cocos2dx 3.0中有一个成员函数叫setTexture
。如果您在场景中保留一个指向精灵对象的成员指针,则可以将您的函数更新为以下内容:
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);
}
}