Cocos2d-x3.6 continuous view random map implementation, cocos2d-x3.6 continuous view

Source: Internet
Author: User

Cocos2d-x3.6 continuous view random map implementation, cocos2d-x3.6 continuous view

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

This section describes the implementation of map initialization.

You can use the following methods to initialize a continuous view map:

We adopt the third method. It should be noted that, to ensure that all the last images are eliminated, all the images should be an even number. We use a grid-by-grid plot. We place two identical images each time. After the map is fully covered, the images are randomly disrupted.

The initialization is as follows:

0 0 0 0 0 0 0 0 0 0
0 1 1 2 2 3 3 4 4 0
0 5 5 6 6 7 7 8 8 0
0 9 9 10 10 11 11 12 12 0
0 13 13 14 14 15 15 16 16 0
0 0 0 0 0 0 0 0 0 0

Then you can randomly disrupt it. Note that only the non-zero position is randomly disrupted.

How to implement it in the code is very simple.

Defines a two-dimensional array. Each map corresponds to a value. The value 0 indicates a space, and other numbers are the image IDs.
The array is initialized to 0 and then traversed once. The four borders are still zero, and the positions in them are assigned values by image id.

I divide the map array assignment and Image Rendering into two parts: Initialize the map array and then draw the board. Check the Code:

Void GameScene: initMap () {// here, x is used to control the value of each cell of the map array, and y is used to control two inversion operations, that is to say, each two cells mark the same value int x = 1; int y = 0; // The array subscript starts from 0, where the traversal starts from 1, then the outermost circle is 0 unchanged for (int I = 1; I <xCount-1; I ++) {for (int j = 1; j <yCount-1; j ++) {// map array value mMap [I] [j] = x; // y controls inversion, and x controls the increase of the value per grid, increase to the total number of images and then increase from 1. if (y = 1) {x ++; y = 0; if (x = iconCount) {x = 1 ;}} else {y = 1 ;}}change (); drawMap ();}

The above change () function is to randomly disrupt the map array, and the drawMap () function is to draw a board, indicating that the corresponding icons of the map array are drawn.

Next, let's look at the change () function:

// Randomly change the game location void GameScene: change () {// Random Seed srand (unsigned int) time (NULL); // temporary variable, used to exchange data in two locations: int tempX, tempY, and tempM; // traverses the map array and randomly exchanges the location for (int x = 1; x <xCount-1; x ++) for (int y = 1; y <yCount-1; y ++) {tempX = 1 + (int) (CCRANDOM_0_1 () * (xCount-2 )); tempY = 1 + (int) (CCRANDOM_0_1 () * (yCount-2); tempM = mMap [x] [y]; mMap [x] [y] = mMap [tempX] [tempY]; mMap [tempX] [tempY] = tempM ;}}

It's easy, right.

Next, let's look at the drawMap () function:

Void GameScene: drawMap () {// draw the genie for (int x = 0; x <xCount; x ++) {for (int y = 0; y <yCount; y ++) {if (mMap [x] [y]> 0) {char iconName [64] = {0 }; // format the image name sprintf (iconName, "mongod.png", mMap [x] [y]); auto position = indextoScreen (x, y ); // All images have been added to the frame cache. Here, we use the name to get a frame and create a Sprite auto icon = Sprite: createWithSpriteFrame (SpriteFrameCache: getInstance () -> getSpriteFrameByName (iconName); // icon-> setAnchorPoint (Vec2 (0, 0); icon-> setPosition (position); // you can specify a tag, to facilitate subsequent identification, click the icon int tag = (yCount-2) * (x-1) + y; // This is the Z order and tag value, set bigger values in the Z sequence to ensure that the icon is in front of other genie addChild (icon, 100, tag );}}}}

The Code has added a lot of comments and should be well understood. However, there is a line of commented-out code that sets the anchor. The default anchor of the genie is (0.5, 0.5), that is, the central position, but I first wanted to set it to the lower left corner, so that the map is more intuitive. After clicking the link, we found that it would be very troublesome to connect from the middle, so we commented out this line and directly used the default anchor. In this case, the screen coordinates and map array coordinates will be processed. I will talk about it later.

Now, the map is drawn.

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.