Cocos2d-X:CCDrawNode绘制圆形/自定义形状

时间:2014-09-19 09:17:01

标签: android c++ iphone xcode cocos2d-x

我正在使用CCDrawNode来创建蒙版类型效果(不完全是蒙版)。一切都运行良好,但有一个问题CCDrawNode只绘制正方形,我想用自定义纹理/精灵绘制它。有没有解决方案。

以下是我使用CCDrawNode

的代码
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    CCLayer *layer = CCLayer::create();
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    layer->addChild(pSprite, 0);
    addChild(layer);

    //this is the layer that we want to "cut"
    CCLayer* layer1 = CCLayerColor::create(ccc4(122, 144, 0, 255), visibleSize.width, visibleSize.height);

    this->setTouchEnabled(true);

    //we need to create a ccnode, which will be a stencil for ccclipingnode, draw node is a good choice for that
    stencil = CCDrawNode::create();

    //CCClipingNode show the intersection of stencil and theirs children
    CCClippingNode *cliper = CCClippingNode::create(stencil);
    cliper->setInverted(true);
    cliper->addChild(layer1);
    addChild(cliper);

    return true;
}


void HelloWorld::ccTouchesMoved(CCSet* touches, CCEvent* event)
{
    CCTouch* touch = (CCTouch*)touches->anyObject();

    // get start & end location
    CCPoint start = touch->getLocationInView();
    CCPoint end = touch->getPreviousLocationInView();

    // get corrected location
    start = CCDirector::sharedDirector()->convertToGL(start);
    end = CCDirector::sharedDirector()->convertToGL(end);

    //stencil->drawDot(start, 25, ccc4f(0, 0, 0, 255));

    stencil->drawSegment(start, end, 25, ccc4f(0, 0, 0, 255));
}

1 个答案:

答案 0 :(得分:0)

如果要绘制自定义纹理,则应使用CCRenderTexture。为了画出一些你应该像这样的东西

myRenderTexture->begin();
mySpriteLoadedWithTexture->visit();
myRenderTexture->end();

此外,如果您希望绘制的线条平滑,则应将其绘制成循环,以便它们以相等的距离放置

float distance = ccpDistance(start, end);
for (int i = 0; i < distance; i++)
{
    float difx = end.x - start.x;
    float dify = end.y - start.y;
    float delta = (float)i / distance;
    m_brush->setPosition(CCPoint(start.x + (difx * delta), start.y + (dify * delta)));
    m_brush->visit();
}

希望有所帮助