【问题标题】:Cocos2d-X: CCDrawNode draw circle/custom shapeCocos2d-X: CCDrawNode 画圆/自定义形状
【发布时间】:2014-09-19 09:17:01
【问题描述】:

我正在使用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));
}

【问题讨论】:

  • 假设它与 cocos2d-iphone 中的类相同,那么 CCDrawNode 不绘制纹理,只绘制(填充)多边形。

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


【解决方案1】:

如果你想绘制自定义纹理,你应该使用 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();
}

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2012-05-13
    相关资源
    最近更新 更多