Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please tell me, if you feel good please support a lot of praise. Thank you! Hopy;)
Disclaimer: All translations provided by this blog are from the Internet and are for learning and communication purposes only. Also, do not remove this statement when reproduced. In case of any dispute, there is no relationship between the blogger and the person who published the translation. Thank you for your cooperation!
Follow the yellow Bricks forward
Now that we've found our path, we just have to let the cat follow it.
What we're going to do next is to remember the whole path and make the cat move in a single step, depending on the path.
Create an array of storage paths in CatSprite.h and add them in the private segment of the Catsprite @interface:
*shortestPath;
Then complete the following modifications in the CATSPRITE.M:
// Add inside the CatSprite private properties and methods section@property (nonatomicNSMutableArray *shortestPath;// After the CatSprite @implementation@synthesize shortestPath;// Inside initWithLayerself.shortestPathnil;// Inside dealloc nil;
Now we're going to create a way to store the entire path and manage the start of the animation, with the following modifications in CATSPRITE.M:
//ADD inside the Catsprite private properties and Methods section- (void) Constructpathandstartanimationfromstep: (Shortestpathstep *) step;//Inside Movetoward, comment out the pathfound BOOL//bool pathfound = NO;//Inside movetoward, replace Pathfound = YES with this:[ SelfConstructpathandstartanimationfromstep:currentstep];//Also comment All of the debugging statements below.//Inside Movetoward, replace if (!pathfound) with this:if( Self. Shortestpath==Nil) {//No path found//Add This new method://Go backward from a step (the final one) to reconstruct the shortest computed path- (void) Constructpathandstartanimationfromstep: (Shortestpathstep *) step{ Self. Shortestpath= [NsmutablearrayArray]; Do{if(Step. Parent!=Nil) {//Don ' t add the last step which are the start position (remember we go backward, so the last one is the Origin Positio n;-)[ Self. ShortestpathInsertobject:step Atindex:0];//Always insert at index 0 to reverse the path} Step = Step. Parent;//Go backward} while(Step! =Nil);//Until There is no more parents for(Shortestpathstep *s in Self. Shortestpath) {NSLog(@"%@", s); }}
Note In the Movetoward method, we called a new method that replaced the original code that printed the results in the console, and we removed the pathfound variable. As usual, The comments in the Constructpathandstartanimationfromstep method explain in detail what actually happened.
Now compile the run, and if you touch the same tile as we said before, you should see the following log:
<shortestpathstep: 0x6b37160>POS=[24;1] G=1 h=4 f=5<shortestpathstep: 0x6b37340>POS=[23;1] g=2 h=3 f=5<shortestpathstep: 0x6b37590>POS=[22;1] g=3 h=2 f=5<shortestpathstep: 0x6b395c0>POS=[21;1] g=4 h=3 f=7<shortestpathstep: 0x6b37ae0>POS=[20;1] g=5 h=4 f=9<shortestpathstep: 0x6b38c60>POS=[20;2] g=6 h=3 f=9<shortestpathstep: 0x6b36510>POS=[20;3] g=7 h=2 f=9<shortestpathstep: 0x6b3b850>POS=[21;3] G=8 h=1 f=9<shortestpathstep: 0x6b3cf30>POS=[22;3] g=9 h=0 f=9
Note that it is similar to before, except now it is from start to finish (reversal of previous results) and the data stored in the array is more convenient for us to use.
The last thing to do is to iterate through the Shortestpath array and animate the path the cat follows. To do this, we will create a method to eject each step of the data from the array so that the cat can move to that location and add a callback method to repeat the method until the path is complete.
Complete the following modifications in CATSPRITE.M:
//ADD inside the Catsprite private properties and Methods section- (void) Popstepandanimate;//Add to bottom of Constructpathandstartanimationfromstep[ SelfPopstepandanimate];//Add New method- (void) popstepandanimate{//Check If there remains path steps to go through if([ Self. ShortestpathCount] = =0) { Self. Shortestpath=Nil;return; }//Get The next step to move toShortestpathstep *s = [ Self. ShortestpathObjectatindex:0];//Prepare the action and the callback IDMoveaction = [Ccmoveto actionwithduration:0.4Position:[_layer positionfortilecoord:s. Position]];IDMovecallback = [Cccallfunc actionwithtarget: SelfSelector@selector(Popstepandanimate)];//Set the method itself as the callback //Remove the step[ Self. ShortestpathRemoveobjectatindex:0];//Play actions[ SelfRunaction:[ccsequence actions:moveaction, Movecallback,Nil]];}
Compile and then run ...
Our cat is automatically moved to the point where you clicked.:-)
How to implement A * pathfinding algorithm in cocos2d game (v)