這是我根據這篇博文來在cocos2dx上編寫,這是最終的效果圖:
紅色的地方是執行軌跡,黑色是禁止區域,接下來是**,請結合那篇博文**:
首先建立地板類,必須要有x和y,還有根據那篇博文的h和g,這是h檔案中的**:
#include "cocos2d.h"
typedef enum _floorstate
floorstate;
class floor : public cocos2d::layercolor
static int at(int x, int y);
static floor * create(const cocos2d::color4b& color);
bool initwithcolor(const cocos2d::color4b& color);
int getf();
private:
cc_synthesize(int, _x, x);
cc_synthesize(int, _y, y);
cc_synthesize(int, _h, h);
cc_synthesize(int, _g, g);
cc_synthesize(floorstate, _landform, landform);
cc_synthesize(floor *, _lastfloor, lastfloor);
};基本上沒有什麼難度,另外,那個靜態方法at使用了從陣列中快速獲取地板。
然後是cpp檔案:
#include "floor.h"
using_ns_cc;
floor::floor()
int floor::at(int x, int y)
floor *floor::create(const cocos2d::color4b &color)
delete _floor;
_floor = null;
return null;
}bool floor::initwithcolor(const cocos2d::color4b &color)
return false;
}int floor::getf()
然後在執行的layer中:
void mainscene::createground()
ground.pushback( _floor );}}
}其中,這個方法:_floor -> setlandform( floorriver )使用來製造禁止區域
然後新增起點和終點:
end = floor::create( color4b(0, 255, 255, 10) );
end -> setposition( (ground.at( floor::at(24, 14) )) -> getposition() );
end -> setx(24);
end -> sety(14);
this -> addchild( end );
origin = floor::create( color4b(255, 255, 0, 255) );
origin -> setposition( (ground.at( floor::at(0, 0) )) -> getposition() );
origin -> setx(0);
origin -> sety(0);
origin -> seth( fabs((end -> getx() - origin -> getx())) + fabs( end -> gety() - origin -> gety() ) );
origin -> setg(0);
this -> addchild( origin );
open.pushback( origin );
在這些完成後,就是需要使用a*演算法來計算出路徑了
首先從open陣列中尋找f最小的地板;之後,將上下左右的地板做個判斷,是否加入陣列中,這部分放在乙個死迴圈中,當加入的地板正是終點時,可以結束:
while (1)
}floor *_origin = (*minf);
handlefloor(_origin, _origin -> getx(), _origin -> gety() - 1);
handlefloor(_origin, _origin -> getx(), _origin -> gety() + 1);
handlefloor(_origin, _origin -> getx() - 1, _origin -> gety());
handlefloor(_origin, _origin -> getx() + 1, _origin -> gety());
open.eraseobject( _origin );
close.pushback( _origin );
_origin -> setcolor( color3b(0, 255, 0) );
floor *getend = open.at( open.size() - 1 );
if ( getend -> getx() == end -> getx() && getend -> gety() == end -> gety() )
}handlefloor函式是用來判斷該地板是否能夠加入陣列中
void mainscene::handlefloor(floor *_floor, int x, int y)
floor *handle = ground.at( floor::at(x, y) );
handle -> setx(x);
handle -> sety(y);
handle -> seth( fabs((end -> getx() - handle -> getx())) + fabs( end -> gety() - handle -> gety() ) );
handle -> setg( fabs((origin -> getx() - handle -> getx())) + fabs( origin -> gety() - handle -> gety() ) );
judge( handle, _floor );
}#define judge(__floor__, __last__) \
if ( !open.contains(__floor__) && !close.contains(__floor__) && __floor__ -> getlandform() != floorriver ) \
\之後,就是取出open陣列終的最後一塊地板,並可以獲取這塊地板的上一塊地板,並將其顏色改變
floor *index = open.at( open.size() - 1 );
while ( index -> getlastfloor() != null )
cocos2d x遊戲例項(6) A星演算法(2)
首先函式從findpath函式開始,初始化的內容如下 首先是把當前位置和目標位置賦給我們這個類的成員變數,然後把我們的地圖變數傳入到成員變數map 中,然後分別初始化 open close path 列表,其中 path 和close 列表先為空,open 列表中為了進行堆排序時方便 堆排序時從索引...
SneakInput在cocos2d x下的示例
看了很多教程和文件,無論2d還是2d x都推薦使用開源的sneakinput作為其觸屏的手柄元件。我的環境為vs2010 cocos2d 1.0.1 x 0.12.0 經過自己的試驗,發現在我的環境下並不需要修改sneakinput的原始碼,將原始碼解壓後,放在自己的專案裡就可以正常使用。sneak...
cocos2d x遊戲例項(5) A星演算法(1)
下面我們就介紹一下a星演算法,他就是一種啟發性的演算法,根據現在到達這個位置的步數及之後的 估計步數 即f g h,f是整個從起點到終點的代價,g是從起點到我們目前位置的步數,h是從目前位置到終點的估計值,注意這裡是估計值,所以我們得到解並不一定是最好的解,具體解 好 到什麼程度呢?就是要根據h的估...