2014-09-19 80 views
0

我正在使用CCDrawNode创建面具类型效果(不完全掩盖)。一切运作良好,但有一个问题,CCDrawNode只绘制广场,我想绘制自定义纹理/精灵。有没有解决办法。Cocos2d-X: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)); 
} 
+0

假设它与cocos2d-iphone中的类相同,那么CCDrawNode不绘制纹理,只绘制(填充)多边形。 – LearnCocos2D 2014-09-19 09:45:25

回答

0

如果你想吸引你应该使用CCRenderTexture自定义纹理的代码。为了画点什么,你应该去smthin这样

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(); 
} 

希望它有帮助