Dijkstra of cocos2dx search path

Source: Internet
Author: User
Tags addchild

The header file and source file can be copied to the project! Have fun

Cocos2dx 3.2 works the same way

# Ifndef _ helloworld_scene_h __# DEFINE _ helloworld_scene_h __# include "cocos2d. H "# include" vector "using namespace STD; using_ns_cc; Class pathsprite: Public cocos2d: SPRITE // inherit the sprite class, because you need to add some other variables in It {pathsprite (): sprite () {m_parent = NULL; m_child = NULL; m_distance = 0;}; public: static pathsprite * Create (const char * Ch) {pathsprite * pret = new pathsprite (); if (pret) {pret-> initwithfile (CH); pret-> autorel Else (); Return pret;} else {Delete pret; pret = NULL; return NULL ;}} pathsprite * m_parent; // parent node pathsprite * m_child; // subnode float m_distance; // The distance from the start point to int m_x; // map coordinate int m_y;}; Class pathsearchinfo // pathing class (mainly responsible for pathfinding parameters and logic) {public: static int m_startx; // start point: static int m_starty; static int m_endx; // end point: static int m_endy; static vector <pathsprite *> m_openlist; // open list (containing adjacent nodes) Static vector <pathsprite *> m_inspectlist; // Detection list (nodes containing all obstacles) Static void barriertest (int x, int y) // simulate obstacle {pathsprite * _ z = getobjbypointofmapcoord (m_inspectlist, X, y); If (_ z) {_ z-> setcolor (cccolor3b: yellow); removeobjfromlist (pathsearchinfo: m_inspectlist, _ z );}} static float calculatetwoobjdistance (pathsprite * obj1, pathsprite * obj2) // calculate the distance between two objects {float _ offsetx = obj1-> m_x-obj2-> m_x; float _ offsety = obj1-> m_y-obj2-> m_y; return SQRT (_ o Ffsetx * _ offsetx + _ offsety * _ offsety);} static void inspecttheadjacentnodes (pathsprite * node, pathsprite * adjacent) // puts adjacent nodes into open nodes {If (adjacent) {adjacent-> m_distance = node-> m_distance + pathsearchinfo: calculatetwoobjdistance (node, adjacent); // obtain the Accumulated distance adjacent-> m_parent = node; // set the parent node-> m_child = adjacent; // set the subnode pathsearchinfo: removeobjfromlist (pathsearchinfo: m_inspectlist, adjacent); // check the Detected Delete pathsearchinfo: m_openlist.push_back (adjacent) from the detection list; // Add to the open list} static pathsprite * getminpathformopenlist () // obtain the minimum path value from the open node {pathsprite * _ sp = * m_openlist.begin (); For (vector <pathsprite * >:: iterator iter = m_openlist.begin (); iter! = M_openlist.end (); ITER ++) {If (* ITER)-> m_distance <_ Sp-> m_distance) {_ sp = * ITER;} return _ sp ;} static pathsprite * getobjbypointofmapcoord (vector <pathsprite *> & spritevector, int X, int y) // obtain the object by vertex {for (INT I = 0; I <spritevector. size (); I ++) {If (spritevector [I]-> m_x = x & spritevector [I]-> m_y = y) {return spritevector [I];} return NULL;} static bool removeobjfromlist (vector <pathsprite *> & spritev Ector, pathsprite * sprite) // remove the object from the container {for (vector <pathsprite *>: iterator iter = spritevector. Begin (); iter! = Spritevector. end (); ITER ++) {If (* iter = sprite) {spritevector. erase (ITER); Return true ;}} return false ;}; class helloworld: Public cocos2d: layer {public: // There's no 'id' in CPP, so we recommend returning the class instance pointer static cocos2d: Scene * createscene (); // here's a difference. method 'init 'in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool Init (); // a selector callback void menuclosecallback (cocos2d: ref * psender ); // implement the "static create ()" method manually create_func (helloworld);}; # endif // _ helloworld_scene_h __

# Include "helloworldscene. H "vector <pathsprite *> pathsearchinfo: m_openlist; vector <pathsprite *> pathsearchinfo: m_inspectlist; int pathsearchinfo: m_startx; int pathsearchinfo: m_starty; int pathsearchinfo: m_endx; int pathsearchinfo: m_endy; SCENE * helloworld: createscene () {// 'Scene 'is an autorelease object auto scene = scene: Create (); // 'player' is an autorelisted object auto layer = helloworld: crere Ate (); // Add layer as a child to scene-> addchild (layer); // return the scene return scene ;} // on "init" you need to initialize your instancebool helloworld: Init () {// 1. super init first if (! Layer: Init () {return false;} size visiblesize = Director: getinstance ()-> getvisiblesize (); vec2 origin = Director: getinstance () -> getvisibleorigin (); size winsize = Director: getinstance ()-> getwinsize (); /// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // Add a "close" icon to exit the progress. it's an autorelsponob Ject // simulate the upper left corner of a map as (0, 0). It is used to simulate tiledmap. The width of each block is 1 int length = 10; for (INT I = 0; I <length; I ++) {for (Int J = 0; j <length; j ++) {pathsprite * _ sp = pathsprite: Create ("closenormal.png"); _ Sp-> m_x = J; _ Sp-> m_y = I; size _ size = _ Sp-> getcontentsize (); _ Sp-> setposition (ccpoint (J * _ size. width + 100,-I * _ size. height + 600); pathsearchinfo: m_inspectlist.push_back (_ Sp); this-> addchild (_ Sp) ;}// sets the obstacle paths Earchinfo: barriertest (); pathsearchinfo: barriertest ); pathsearchinfo: barriertest ); pathsearchinfo: barriertest (2, 7); pathsearchinfo: barriertest (3, 7); // pathspr ITE: getobjbypointofmapcoord (m_inspectlist, 2, 5)-> removefromparent (); // set the start and end pathsearchinfo: m_startx = 0; pathsearchinfo: m_starty = 1; pathsearchinfo :: m_endx = 4; pathsearchinfo: m_endy = 9; // get the Start Node pathsprite * _ sp = pathsearchinfo: Unknown (pathsearchinfo: m_inspectlist, pathsearchinfo: m_startx, pathsearchinfo:: m_starty); // set the distance from the start point to 0_sp-> m_distance = 0; // Delete the detected vertex from the detection list Pathsearchinfo: removeobjfromlist (pathsearchinfo: m_inspectlist, _ Sp); // Add the open list pathsearchinfo: m_openlist.push_back (_ Sp); pathsprite * _ node = NULL; while (true) {// obtain the vertex _ node = pathsearchinfo: getminpathformopenlist (); If (! _ Node) {// The path break cannot be found;} // Delete the computed vertex from the open list pathsearchinfo: removeobjfromlist (pathsearchinfo: m_openlist, _ node ); int _ x = _ node-> m_x; int _ y = _ node-> m_y; // If (_ x = pathsearchinfo: m_endx & _ y = pathsearchinfo:: m_endy) {break;} // checks whether adjacent nodes in eight directions can be placed in the open list. pathsprite * _ adjacent = pathsearchinfo: getobjbypointofmapcoord (pathsearchinfo: m_inspectlist, _ x + 1, _ y + 1); pathsearchinfo: inspecttheadjacentnodes (_ node, _ adjacent); _ adjacent = pathsearchinfo: getobjbypointofmapcoord (pathsearchinfo: m_inspectlist, _ x + 1, _ y); pathsearchinfo: inspecttheadjacentnodes (_ node, _ adjacent); _ adjacent = pathsearchinfo: getobjbypointofmapcoord (pathsearchinfo: m_inspectlist, _ x + 1, _ Y-1); pathsearchinfo: inspecttheadjacentnodes (_ node, _ adjacent); _ adjacent = pathsearchinfo: Unknown (pathsearchinfo: m_inspectlist, _ x, _ y-1); pathsearchinfo:: inspecttheadjacentnodes (_ node, _ adjacent); _ adjacent = pathsearchinfo: getobjbypointofmapcoord (pathsearchinfo: m_inspectlist, _ x-1, _ y-1); pathsearchinfo :: inspecttheadjacentnodes (_ node, _ adjacent); _ adjacent = pathsearchinfo: callback (pathsearchinfo: m_inspectlist, _ x-1, _ y); pathsearchinfo: inspecttheadjacentnodes (_ node, _ adjacent); _ adjacent = pathsearchinfo: locate (pathsearchinfo: m_inspectlist, _ x-1, _ y + 1); pathsearchinfo: inspecttheadjacentnodes (_ node, _ adjacent ); _ adjacent = pathsearchinfo: substring (pathsearchinfo: m_inspectlist, _ x, _ y + 1); pathsearchinfo: inspecttheadjacentnodes (_ node, _ adjacent);} int I = 0; while (_ node) {pathsprite * _ sp = _ node; _ node = _ node-> m_parent; _ Sp-> setcolor (cccolor3b: magenta);} return true ;} void helloworld: menuclosecallback (ref * psender) {# If (cc_target_platform = platform) | (cc_target_platform = cc_platform_winrt) MessageBox ("you pressed the close button. windows Store apps do not implement a close button. "," alert "); return; # endif Director: getinstance ()-> end (); # If (cc_target_platform = cc_platform_ios) Exit (0); # endif}


Dijkstra of cocos2dx search path

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.