Cocos2dx3.2 use Crazy Tetris to draw irregular square masks (ClippingNode)
I have already considered possible elimination and area determination problems. The next question is how to display these irregular images due to elimination.
Here, I use ClippingNode (mask ). The introduction to ClippingNode on the Internet is very careful, so here I will just give a brief introduction:
Just like his name, he is also a node, so you can refer to my first blog post on the node tree, you need to add it to another node.
When using the template, you must add the template and baseboard to it. Stencel means something like a mold. As you can imagine, if we define the shape of the mold and print it on the bottom plate with a large pattern, only this part of the mold Shape on the bottom plate is displayed, or the rest of the mold Shape is exclusive.
Method for adding a template:
static ClippingNode* create(Node *stencil);
The construction method with a template directly;
void setStencil(Node *stencil);
Add Template
Add the baseboard directly using the AddChild method.
To put it simply, you can refer to the online materials for other functions.
Here, because we can know the vertex information of the image to be displayed, we can easily construct a template to display irregular images. Of course, this piece of code should be put in the initialization function of the BaseBlock block class to enable it to automatically complete the mask each time it is initialized.
The Code is as follows:
void BaseBlock::initForm(std::vector
* shapeVecs, std::vector
* shapeVecAmount, int shapeAmount, Color4B color){Vec2 origin = Director::getInstance()->getVisibleOrigin();this->shapeAmount = shapeAmount;this->shapeVecAmount = shapeVecAmount;this->shapeVecs = shapeVecs;auto stencil = DrawNode::create();auto body = PhysicsBody::create();for(int i=0; i
at(i), shapeVecAmount->at(i));shape->setRestitution(0.5);body->addShape(shape);stencil->drawPolygon(shapeVecs->at(i), shapeVecAmount->at(i), Color4F(1, 1, 0, 1), 0, Color4F(1, 1, 0, 1));}auto clipper = ClippingNode::create();clipper->setZOrder(0);clipper->setStencil(stencil);stencil->setPosition(stencil->getPositionX() + this->getContentSize().width/2, stencil->getPositionY() + this->getContentSize().height/2);this->color = color;auto back = LayerColor::create(this->color, this->getTextureRect().getMaxX(), this->getTextureRect().getMaxY());clipper->addChild(back);this->addChild(clipper);this->setPhysicsBody(body);}
I am using Cocos2dx3.2 to create directories for other game-related blogs to create the gravity version of Tetris (Crazy RIS)