2014-09-19 78 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(); 
} 

希望它有幫助