Cocos2d-x 3.2 Monopoly game project development-Part 8 role walking by path, cocos2d-x Project Development

Source: Internet
Author: User

Cocos2d-x 3.2 Monopoly game project development-Part 8 role walking by path, cocos2d-x Project Development

After obtaining the path, we can let the role walk by path. When you click the go button, we call the startGo () method of player, the input parameter stores two one-dimensional arrays of the path.

Void GameBaseScene: goButtonCallback (cocos2d: CCObject * pSender) {RouteNavigation: getInstance ()-> getPath (player1, 3, canPassGrid, summary, tiledColsCount); std :: vector <int> colVector = RouteNavigation: getInstance ()-> getPathCols_vector (); std: vector <int> rowVector = RouteNavigation: getInstance ()-> getPathRow_vector (); for (int I = 0; I <rowVector. size (); I ++) {log ("rowVector row is % d --- colVector col is % d", rowVector [I], colVector [I]);} // call the startGo method player1 of the RicherPlayer class-> startGo (rowVector, colVector );}


Add the corresponding startGo method to the RicherPlayer class.

Void RicherPlayer: startGo (std: vector <int> rowVector, std: vector <int> colVector) {// gets the game controller RicherGameController and calls the startRealGo () method, start real role walking RicherGameController * rgController = RicherGameController: create (); addChild (rgController); rgController-> startRealGo (rowVector, colVector, this );}

Void RicherGameController: startRealGo (std: vector <int> rowVector, std: vector <int> colVector, RicherPlayer * richerPlayer) {currentRow = rowVector [0]; currentCol = colVector [0]; // obtain the row and column values of the first position nextRow = 0; nextCol = 0; // create the actions of the row and column values of the next step, and put it into the cache if (! AnimationCache: getInstance ()-> animationByName ("left_animation") {AnimationCache: getInstance ()-> addAnimation (Animation: createWithSpriteFrames (richerPlayer-> developers (), playerGoPerFrameTime), "left_animation");} if (! AnimationCache: getInstance ()-> animationByName ("right_animation") {AnimationCache: getInstance ()-> addAnimation (Animation: createWithSpriteFrames (richerPlayer-> developers (), playerGoPerFrameTime), "right_animation");} if (! AnimationCache: getInstance ()-> animationByName ("down_animation") {AnimationCache: getInstance ()-> addAnimation (Animation: metrics (richerPlayer-> getAnim_down_vector (), playerGoPerFrameTime), "down_animation");} if (! AnimationCache: getInstance ()-> animationByName ("up_animation") {AnimationCache: getInstance ()-> addAnimation (Animation: metrics (richerPlayer-> getAnim_up_vector (), playerGoPerFrameTime), "up_animation");} // obtain the action from the cache, and create the animation left = Animate: create (AnimationCache: getInstance () -> animationByName ("left_animation"); right = Animate: create (AnimationCache: getInstance ()-> animationByName ("right_animation"); down = Animate :: create (AnimationCache: getInstance ()-> animationByName ("down_animation"); up = Animate: create (AnimationCache: getInstance () -> animationByName ("up_animation"); // retain, reference count plus one to prevent animation from being cleared left-> retain (); right-> retain (); down-> retain (); up-> retain (); // assign a value to the corresponding Variable _ rowVector = rowVector; _ colVector = colVector; _ richerPlayer = richerPlayer; steasgone = 0; // The role has taken a few steps. stepsCount = _ rowVector. size ()-1; // number of steps to be taken to obtain the path. Because the first step is the row and column of the current position, the number of steps is not counted as moveOneStep (); // start to walk. First step, after completing one step, go to the next step}

Void RicherGameController: moveOneStep () {// obtain the next row and column, and calculate the difference value from the current row and column nextRow = _ rowVector [steasgone + 1]; nextCol = _ colVector [steasgone + 1]; int distanceRow = nextRow-currentRow; int distanceCol = nextCol-currentCol; MoveBy * moveBy; Repeat * repeate; Action * spawnAction; // create the upper, lower, and lower actions based on the row and column differences, animations including movement and walking, if (distanceRow> 0) // up {moveBy = MoveBy: create (playerGoTotalTime, ccp (0, tiledHeight); repeate = Repeat :: create (up, 1);} if (distanceRow <0) // down {moveBy = MoveBy: create (playerGoTotalTime, ccp (0,-tiledHeight); repeate = Repeat:: create (down, 1);} if (distanceCol> 0) // right {moveBy = MoveBy: create (playerGoTotalTime, ccp (tiledWidth, 0); repeate = Repeat:: create (right, 1);} if (distanceCol <0) // left {moveBy = MoveBy: create (playerGoTotalTime, ccp (-tiledWidth, 0 )); repeate = Repeat: create (left, 1) ;}// create a synchronous animation. After the animation is moved, run the callEndGoFunc method and call the endGo () method spawnAction = Sequence :: create (Spawn: create (moveBy, repeate, NULL), callEndGoFunc, NULL); _ richerPlayer-> runAction (spawnAction );}

Void RicherGameController: endGo () {steasgone ++; // after completing the step, add 1 If (steasgone> = stepsCount) // if the number of steps is greater than or equal to the total number of steps, returns {return;} currentRow = nextRow; currentCol = nextCol; // The current row and column are assigned the next row of moveOneStep (); // start the next step of moving log ("go end ");}


After testing, we found that the role will move back and forth, and then come back. Therefore, we need to add an attribute to the role Player to indicate the position from which the role came from.

Modify the getPath () method of the RouteNavigation class

Void RouteNavigation: getPath (RicherPlayer * player, int stepsCount, bool ** canPassGrid, int gridRowsCount, int gridColsCount ){................................ Int rowtemp = player-> getComeFromeRow (); int coltemp = player-> getComeFromCol (); if (rowtemp <=-1 | coltemp <=-1) {player-> setComeFromCol (currentCol); player-> setComeFromeRow (currentRow);} // you can specify where the role comes from as false, it indicates that canPassGrid_copy [player-> getComeFromeRow ()] [player-> getComeFromCol ()] = false ;............................. // After obtaining the path, set the role's source location player-> setComeFromCol (pathCols_vector [pathCols_vector.size ()-2]); player-> setComeFromeRow (pathRow_vector [pathRow_vector.size () -2]);}

The test showed that the role could finally move normally.




The flowchart is as follows:


So far, code has been written too much, so it is necessary to reorganize it. In the next section, we will perform code optimization. This facilitates subsequent development.


Click to download code http://download.csdn.net/detail/lideguo1979/8292407

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.