Cocos2d-x 3.2 Monopoly game project development-Part 7 get role PATH

Source: Internet
Author: User

Cocos2d-x 3.2 Monopoly game project development-Part 7 get role PATH

Create a RouteNavigation class and define the getPath () method to obtain the role path. We design this class in the single-sample mode. First, let's look at the definition of this class.

 

RouteNavigation. hclass RouteNavigation {public: static RouteNavigation * routeNav_Instance; // static RouteNavigation * getInstance (); // obtain the static object method void getPath (Sprite * playerSprite, int stepsCount, bool ** canPassGrid, int gridRowsCount, int gridColsCount); // defines the method for obtaining paths. protected: RouteNavigation (void );~ RouteNavigation (void );};

RouteNavigation.cppRouteNavigation::~RouteNavigation(void){routeNav_Instance = NULL; }RouteNavigation* RouteNavigation::getInstance(){if(!routeNav_Instance){          routeNav_Instance = new RouteNavigation();      }      return routeNav_Instance;  

 

After defining the class, start to implement the getPath () method. Remember the previous getPath process diagram. I will write this method according to the previous process.

 

Parameter description: playerSprite: The role whose path is to be obtained, that is, the role that calls the getPath method, transmits itself to stepsCount: the number of steps the role should take canPassGrid: two-dimensional array gridRowsCount: number of rows in the canPassGrid array gridColsCount: Number of columns in the canPassGrid array void overflow: getPath (Sprite * playerSprite, int stepsCount, bool ** canPassGrid, int gridRowsCount, int gridColsCount) {// defines a vector one-dimensional array to store the row and column of the obtained path. First, clear pathCols_vector.clear (); pathRow_vector.clear (); // defines the current row and column of the role, int nextCol, nex TRow; int currentCol, currentRow; // gets the coordinate value float x = playerSprite-> getPositionX (); float y = playerSprite-> getPositionY (); // assign values to the column and column variables starting with the role based on the current coordinate value of the role. It is the coordinate divided by the wide and high values of each row and column currentCol = x/tiledHeight; // we used to display the role in the center, in the GameBaseScene: addPlayer () method, add the vertical position + tiledHeight to the role. In this case, the correct number of rows is obtained: currentRow = (y-tiledWidth)/tiledWidth; // defines canPassGrid_copy, the value bool ** canPassGrid_copy = new bool * [gridRowsCount]; for (int row = 0; row
 
  
Dire_4 _4; // create a canPassDirVector_temp location where the current location can pass std: vector
  
   
CanPassDirVector_temp; int hasGoneNumber = 0; // start the loop to find the value of the row and column that can go in each step while (hasGoneNumber
   
    
(Dire_4 _4). swap (direction_4); std: vector
    
     
(CanPassDirVector_temp). swap (canPassDirVector_temp );}
    
   
  
 


 

 

Let's take a look at how the isCanGoByColRow () method is used to determine whether the current position is up, down, left, and right. The logic is very simple. It is to judge whether the row and column of the Two-dimensional array canPassGrid are true based on the input direction. If it is true, it means that bool RouteNavigation: isCanGoByColRow (int row, int col, int ction, bool ** canPassGrid) {switch (direction) {case GO_UP: {return canPassGrid [row-1] [col];} case GO_DOWN: {return canPassGrid [row + 1] [col];} case GO_LEFT: {return canPassGrid [row] [col-1];} case GO_RIGHT: {return canPassGrid [row] [col + 1] ;}} return false;


 

 

Okay, let's modify the go button and test the obtained path void GameBaseScene: addGoButton () {// modify the Go button to menu Menu * menu = Menu :: create (); menu-> setPosition (CCPointZero); // call the goButtonCallback method MenuItemImage * goMenuItemButton = MenuItemImage: create (map/go_normal.png, map/go_press.png, this, menu_selector (GameBaseScene: goButtonCallback); goMenuItemButton-> setPosition (ccp (tableStartPosition_x + 2 * tableWidth, tableStartPosition_y-tableHeight * 6); menu-> addChild (goMenuItemButton ); addChild (menu);} void GameBaseScene: goButtonCallback (cocos2d: CCObject * pSender) {log (go button clicked); // obtain the RouteNavigation path in step 5 first :: getInstance ()-> getPath (player1, 5, canPassGrid, tiledRowsCount, tiledColsCount); std: vector
 
  
ColVector = RouteNavigation: getInstance ()-> getPathCols_vector (); std: vector
  
   
RowVector = RouteNavigation: getInstance ()-> getPathRow_vector (); // print the output path for (int I = 0; I
   
    

 

Test results: the current position can be displayed on the left, right, or top of the obtained path.

 

 

 

Related Article

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.