Cocos2d-x 3.2 Monopoly Game project development-part seventh get the role path _3

Source: Internet
Author: User

Click Download code http://download.csdn.net/detail/lideguo1979/8291803


Create a new class routenavigation, and define the GetPath () method. To get the role path, we use a singleton pattern to design the class, first look at the definition of the class

Routenavigation.hclass routenavigation{public:static routenavigation* routenav_instance;  The class static object static routenavigation* getinstance ();//Gets the static object method void GetPath (sprite* playersprite,int stepscount,bool** Canpassgrid,int gridrowscount,int Gridcolscount);//define the method to get the path 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;  

Once you've defined the class, start implementing the GetPath () method, and remember the previous GetPath flowchart. I'll start writing this method as I did earlier.

Description: Playersprite: To get the role of the path, that is, which role calls the GetPath method, you pass in the Stepscount: How many steps does a character have to walk Canpassgrid: whether the level map can walk the number of rows in a two-dimensional array Gridrowscount:canpassgrid array Gridcolscount:canpassgrid array of columns void Routenavigation::getpath (sprite* playersprite,int stepscount,bool** canpassgrid,int gridRowsCount,int gridColsCount ) {//defined vector one-dimensional array to hold the obtained path row and column we first clear the pathcols_vector.clear ();p athrow_vector.clear ();//define the role of the current row. Next to the column int nextcol, nextrow;int currentcol,currentrow;//Gets the coordinate value of the current position of the character float x = Playersprite->getpositionx (); Float y = playersprite->getpositiony ();//Assign values to the row and column variables that begin with the role's current coordinate value. The coordinates are divided by the width and height of each row Currentcol = x/tiledheight;//We used to center the character in the Gamebasescene:: Addplayer () method. To the role portrait position + Tiledheight, here to lose, talent get the correct number of rows CurrentRow = (y-tiledwidth)/tiledwidth;//definition canpassgrid_copy.  Receive the value in the Canpassgrid two-dimensional array passed in bool** canpassgrid_copy = new Bool*[gridrowscount]; for (int row = 0;row<gridrowscount;row++) {for (int col = 0;col<gridcolscount;col++) {Canpassgrid_copy[row][col] = Canpassgrid[row][col];}} Create a one-dimensional array direction_4[] values indicate when the forwardThe column position of the upper and lower left and right four adjacent positions can walk std::vector<bool> direction_4;//established Canpassdirvector_temp store the current position up and down can pass the position std::vector< int> Canpassdirvector_temp;int Hasgonenumber = 0;//starts the loop to find the row and column values for each step while (Hasgonenumber<stepscount) {//empties the array first. Revert to the default value Falsedirection_4.clear (); for (int i=0;i<4;i++) {direction_4.push_back (false);} Canpassdirvector_temp.clear ();//Find the current row and column position in the upper and lower left and right four directions, see if it can pass, and assign a value of direction_4 corresponding to TRUE or falsedirection_4[go_up] = Iscangobycolrow (currentrow,currentcol,go_up,canpassgrid_copy);d Irection_4[go_down] = IsCanGoByColRow (CurrentRow, currentcol,go_down,canpassgrid_copy);d Irection_4[go_left] = Iscangobycolrow (Currentrow,currentcol,go_left, canpassgrid_copy);d Irection_4[go_right] = Iscangobycolrow (currentrow,currentcol,go_right,canpassgrid_copy);// Traverse the Direction_4, find the location that can be passed, and deposit in canpassdirvector_temp for (int i=0;i<4;i++) {if (Direction_4[i]) {Canpassdirvector_ Temp.push_back (i);}} a direction int _rand = rand ()%canpassdirvector_temp.size () is randomly taken from one-dimensional array canpassdirvector_temp that the record can pass; Depending on the direction, get the next row and column value switch (CANPASsdirvector_temp[_rand]) {case go_up:{nextrow = currentRow-1; Nextcol = Currentcol; break;}                    Case go_down:{NextRow = CurrentRow +1; Nextcol = Currentcol;break;}                     Case go_left:{NextRow = CurrentRow; Nextcol = Currentcol-1;break;}                    Case go_right:{nextrow = CurrentRow; Nextcol = Currentcol + 1;break;}} Switch infers the direction and assigns values to the next column, Pathcols_vector.push_back (nextcol);p Athrow_vector.push_back (nextRow) in the path array;// Let the current row. False to indicate that it has passed and is not able to walk again.      Prevent characters from pacing canpassgrid_copy[currentrow][currentcol] = false;//Let the current row and column values point to the next row and column position, prepare to find the path row from the next position currentcol = Nextcol; CurrentRow = nextrow;//Step number plus 1. Start looking for the next available row hasgonenumber++;} After the path is found. Perform memory cleanup of related variables Cc_safe_delete (canpassgrid_copy);d irection_4.clear (); Canpassdirvector_temp.clear (); std::vector <bool> (direction_4). Swap (Direction_4);std::vector<int> (canpassdirvector_temp). Swap ( CANPASSDIRVECTOR_TEMP);}


Take a look at how the Iscangobycolrow () method infers whether the current position is up or down.

Logic is very easy, is based on the direction of passing in, inferred that the two-dimensional array canpassgrid corresponding column is true. Suppose true to indicate the ability to pass a bool Routenavigation::iscangobycolrow (int row,int col,int direction,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;



All right. Let's change the Go button. Test is obtained by the path void Gamebasescene::addgobutton () {//Changed the Go button changed to menu menu* menu = Menu::create (); Menu->setposition (     Ccpointzero); Go to 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");// Let's get the path to walk 5 steps routenavigation::getinstance ()->getpath (player1,5,canpassgrid,tiledrowscount,tiledcolscount); std: :vector<int> colvector = routenavigation::getinstance ()->getpathcols_vector ();std::vector<int> Rowvector = Routenavigation::getinstance ()->getpathrow_vector ()//Print out path for (int i=0;i<rowvector.size (); i++) { Log ("Rowvector row is%d---colvector col is%d", rowvector[i],colvector[i]);} .....................} 

Test result, get path show current position can go left, right, up.


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbglkzwd1bze5nzk=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


To be continued .......


Cocos2d-x 3.2 Monopoly Game project development-part seventh get the role path _3

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.