1. 程式人生 > >cocos2dx 3.2 裁剪節點 ClippingNode

cocos2dx 3.2 裁剪節點 ClippingNode

//
//[1].背景圖片(Layer層中)
    Sprite* bg = Sprite::create("HelloWorld.png");
    bg->setPosition(visibleSize / 2);
    this->addChild(bg);
 
 
//[2].建立裁剪節點 : holesClipper
    holesClipper = ClippingNode::create();
    holesClipper->setPosition(visibleSize / 2);
    this->addChild(holesClipper);
 
    //屬性設定
    holesClipper->setInverted(true);        //倒置顯示,未被裁剪下來的剩餘部分
    holesClipper->setAlphaThreshold(0.5f);  //設定alpha透明度閘值
    holesClipper->runAction(RepeatForever::create(RotateBy::create(1, 45))); //旋轉動作
 
 
//[3].建立模板 : holesStencil
    holesStencil = Node::create();
    holesClipper->setStencil(holesStencil); //設定模板節點
 
    //新增一個模板遮罩 ball
    holesStencil->addChild(Sprite::create("ball.png"), -1);
 
 
//[4].建立底板 : holes
    holes = Node::create();
    holesClipper->addChild(holes); //設定底板
 
    //新增另一個底板內容 blocks
    Sprite* content = Sprite::create("blocks.png");
    holesClipper->addChild(content, -1, "content");
 
 
//[5].觸控事件
    auto listener = EventListenerTouchAllAtOnce::create();
    listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
//