Cocos2d-x3.6 connection to see the draw line, cocos2d-x3.6 connection

Source: Internet
Author: User

Cocos2d-x3.6 connection to see the draw line, cocos2d-x3.6 connection

My blog: http://blog.csdn.net/dawn_moon

On the Internet, we can see that many people do not even draw a line of video. In fact, the connection is quite simple. The cocos2d-x provides a very convenient class for drawing graphics, DrawNode. This class encapsulates a lot of lines and polygon methods, which are very convenient, easy to use and powerful.

DrawNode is easy to use. I use a member variable to save this DrawNode because it will be used repeatedly.

mDraw = DrawNode::create();addChild(mDraw, 1000);

Its usage is the same as that of the Genie. After it is created, it is added to the current layer.

Void GameScene: drawLine () {// draw line if (mPath. size ()> = 2) {Vec2 * vecs = new Vec2 [mPath. size ()]; for (int I = 0; I <mPath. size (); I ++) {vecs [I] = indextoScreen (mPath. at (I ). x, mPath. at (I ). y);} // random color of the line auto color = CCRANDOM_0_1 (); // draw a line segment cyclically. Only the line segment can be set to the line width for (int I = 0; I <mPath. size (); I ++) {if (I> 0) {mDraw-> drawSegment (vecs [I-1], vecs [I], 5, Color4F (color, color, color, 1) ;}// map array corresponding to the two connected icons sets 0 Vec2 p1 = mPath. front (); mMap [(int) p1.x] [(int) p1.y] = 0; Vec2 p2 = mPath. back (); mMap [(int) p2.x] [(int) p2.y] = 0; // delete the vertex array of the Line Segment delete [] vecs; // clear the connected icon, clear the path scheduleOnce (SEL_SCHEDULE (& GameScene: clearMatched), 0.2 );}}

As mentioned above, the key points of the path connecting the two icons exist in a container mPath, with at least two points and a maximum of four points.

Then we use the drawSegment () function in DrawNode to draw a line.

This function draws a line segment, which is a line between two points. Why should we use this function. There is also a function in DrawNode that can be used to draw polygon. The prototype is as follows:

void drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color);

This function parameter has a bool closePolygon, which can be used to draw closed or open polygon. This is just a step in place. However, it cannot set the line width. The drawn lines are very fine and the default width is not suitable.

Then I used a function to draw a line segment, and took out the points in the path cyclically. The two points drew a line segment, prototype:

void drawSegment(const Vec2 &from, const Vec2 &to, float radius, const Color4F &color);

The first parameter, the start vertex, the second parameter, the destination vertex, the third parameter, the line width, the fourth parameter, and the line color.

After the link is drawn, set the array of the two icons to 0, indicating that the two icons should have been removed, which is a space.

Delete the vertex array and call the cleanup icon function.

The basic function of this continuous view is complete.

There will be a countdown later, and two functions will be automatically prompted, so a complete connection is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.