Cocos2d-x 3.2 Monopoly game project development-Part 7 get role PATH _ 1, cocos2d-x_1

Source: Internet
Author: User

Cocos2d-x 3.2 Monopoly game project development-Part 7 get role PATH _ 1, cocos2d-x_1

The following design is a little cumbersome and it is necessary to clarify the concept.

In the next step, click the Go button to obtain the random number of the dice, that is, the number of walking steps. The path of the walking is obtained based on the number of steps, and then the role moves the position based on the path. General process:



In this case, it is necessary to clarify the method for getPath () to obtain the path. Let's look at the process diagram.




The general idea is that there may be some differences in code implementation, but it does not affect our overall design philosophy.


Let's start modifying the GameBaseScene code.


GameBaseScene. h // the newly added variable indicates the width and height of each block in the map block. const int tiledWidth = 32; const int tiledHeight = 32; class GameBaseScene: public Layer {public: int tiledColsCount; // int tiledRowsCount; // bool ** canPassGrid; // a two-dimensional virtual void initTiledGrid () array created based on the total number of columns of the map (); // used to initialize the canPassGrid array void setWayPassToGrid (); // set the value of canPassGrid to truevoid onExit () based on the map layer way (); // void addGoButton () needs to be released when exiting; // Add GO button };


Modify the GameBaseScene. cpp File


Add the newly added function to the init method and call bool GameBaseScene: init (){....................... AddGoButton (); initTiledGrid (); setWayPassToGrid ();................} Void GameBaseScene: setWayPassToGrid () {TMXLayer * wayLayer = _ map-> layerNamed ("way"); // obtain the map way layer Size _ mapSize = wayLayer-> getLayerSize (); // obtain the path layer size // obtain the path coordinates and convert them to the row and column values of the map based on the way layer, and set the row and column values of canPassGrid to true, the for (int j = 0; j <_ mapSize. width; j ++) {for (int I = 0; I <_ mapSize. height; I ++) {Sprite * _ sp = wayLayer-> tileAt (Point (j, I); if (_ sp) {float x = _ sp-> getPositionX (); float y = _ sp-> getPositionY (); int col = x/tiledWidth; int row = y/tiledHeight; canPassGrid [row] [col] = true; log ("canPassGrid row = % d, col = % d, canpass = % d", row, col, canPassGrid [row] [col]) ;}} log ("setWayPassToGrid finished ");}


// This is the simple add Go button void GameBaseScene: addGoButton () {Sprite * goButton = Sprite: create (GO_BUTTON ); goButton-> setPosition (ccp (tableStartPosition_x + 2 * tableWidth, tableStartPosition_y-tableHeight * 5); addChild (goButton);} // When exit is called, void GameBaseScene: onExit () {CC_SAFE_DELETE (canPassGrid); Layer: onExit ();}


Next, let's take a look at the implementation of the initTiledGrid () method. Because the map size of each level is different, the implementation of this method is placed in the subclass.

 

The implementation of SeaScene. cpp is as follows:

SeaScene implements the initTiledGrid method void GameBaseScene of the parent class: initTiledGrid () {tiledColsCount = 20; // The total number of map columns tiledRowsCount = 15; // total number of map rows // create a two-dimensional array named canPassGridcanPassGrid = new bool * [tiledRowsCount]; for (int I = 0; I <tiledRowsCount; I ++) based on the number of rows) canPassGrid [I] = new bool [tiledColsCount]; // set the default value of canPassGrid to false, indicating that the for (int row = 0; row <tiledRowsCount; row ++) cannot be passed) {for (int col = 0; col <tiledColsCount; col ++) {canPassGrid [row] [col] = false ;}} log ("");}


OK. Now we can debug the code. From the log, we can see that the row and column values that can be passed in the canPassGrid array have been printed.

Compare the way layer in the sea. tmx file to see if the row and column values are consistent.



Click to download the code

Http://download.csdn.net/detail/lideguo1979/8276135


The retrieval path is a little more complex. We will introduce it in multiple chapters.


To be continued

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.