Cocos2d-x-如何设置CCLayer透明的一部分? [英] Cocos2d-x - how to set part of CCLayer transparent?

查看:45
本文介绍了Cocos2d-x-如何设置CCLayer透明的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 cocos2d-x 的新手,需要您的帮助.

I'm newbie in cocos2d-x and I need your help.

我需要使该图层的触摸部分透明.

I need to make transparent a touched portion of the layer.

如何使图层的一部分透明?我曾考虑使用ССClippingNode,但找不到示例或文档.

How to make a portion of the layer transparent? I had thought to use ССClippingNode, but I'm not find examples or docs.

我使用C ++.谢谢.

I use C++. Thanks.

推荐答案

在TestCpp中,已将项目添加到所有cocos2d-x版本中,您可以找到CCClipingNode的示例.

In TestCpp, project that was added to all cocos2d-x version, you can find examples of CCClipingNode.

如果要使用CCClipingNode隐藏CCNode的一部分(例如图层"),则应将图层添加到CCClipingNode.

If you want to hide part of CCNode(for example "layer") using CCClipingNode, you should add your layer to CCClipingNode.

这是您可以粘贴到HelloWorld初始化中的示例:

This is the example that you can paste in the HelloWorld init:

bool HelloWorld::init()
{

    if ( !CCLayer::init() )
    {
        return false;
    }

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    addChild(CCLayerColor::create(ccc4(122, 144, 0, 255), visibleSize.width, visibleSize.height));

    //this is the layer that we want to "cut"
    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);

    //we need to create a ccnode, which will be a stencil for ccclipingnode, draw node is a good choice for that
    CCDrawNode * stecil = CCDrawNode::create();
    stecil->drawDot(ccp(visibleSize.width/2 + origin.x - 100, visibleSize.height/2 + origin.y), 30, ccc4f(0, 0, 0, 255));
    stecil->drawSegment(ccp(0, 0), ccp(visibleSize.width, visibleSize.height), 20, ccc4f(0, 0, 0, 255));

    //CCClipingNode show the intersection of stencil and theirs children
    CCClippingNode *cliper = CCClippingNode::create(stecil);
    //you want to hide intersection so we setInverted to true
    cliper->setInverted(true);
    cliper->addChild(layer);
    addChild(cliper);

    return true;
}

这篇关于Cocos2d-x-如何设置CCLayer透明的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆