Cocos2d-x 3.2 大富翁遊戲項目開發-第七部分 擷取角色路徑_1,cocos2d-x_1

來源:互聯網
上載者:User

Cocos2d-x 3.2 大富翁遊戲項目開發-第七部分 擷取角色路徑_1,cocos2d-x_1

下面的設計稍微繁瑣一些,有必要把思路說清楚。

下一步的主要實現當我們點擊Go按鈕後,得到骰子隨機數,就是行走的步數,根據步數擷取行走的路徑,然后角色根據路徑移動位置。大體流程:



這其中getPath()擷取路徑的方法有必要說清楚,還是看流程圖吧




思路大體是這樣的,代碼實現上可能存在一些差別,但是不影響我們的整體設計思想。


我們開始修改GameBaseScene代碼


GameBaseScene.h //新添加變數 表示地圖塊中每塊的寬高const int tiledWidth = 32; const int tiledHeight = 32;class GameBaseScene: public Layer{public:int tiledColsCount; //關卡地圖總的列數int tiledRowsCount;//關卡地圖總的行數bool** canPassGrid;//根據地圖總的行列數建立的二維數組virtual void initTiledGrid();//用來初始化canPassGrid數組void setWayPassToGrid();//根據地圖圖層way,設定canPassGrid相應的值為truevoid onExit();//退出時需要釋放某些變數空間void addGoButton();//添加GO 按鈕};


修改GameBaseScene.cpp檔案


在init方法中添加新增函數的調用bool GameBaseScene::init(){ …………………..addGoButton();initTiledGrid();setWayPassToGrid();…………….}void  GameBaseScene::setWayPassToGrid(){TMXLayer* wayLayer = _map->layerNamed("way");//擷取地圖way圖層Size _mapSize = wayLayer->getLayerSize(); //擷取way圖層大小              //根據way圖層,擷取道路的座標並轉換成地圖的行列值,設定canPassGrid相應的行列值為true,表示人物可以從這裡通過               for (int j = 0;  j < _mapSize.width; j++) {  for (int i = 0;  i < _mapSize.height; i++) {                                      Sprite* _sp = wayLayer->tileAt(Point(j, i));                                       if (_sp) {                                                              float x = _sp->getPositionX();float y = _sp->getPositionY();int col = x/tiledWidth;int row = y/tiledHeight;canPassGrid[row][col] = true;log("canPassGrid row=  %d ,col =%d ,canpass = %d" ,row,col,canPassGrid[row][col]);            }          }      }  log("setWayPassToGrid finished");}


//這個就是簡單 的添加Go按鈕void  GameBaseScene::addGoButton(){Sprite* goButton = Sprite::create(GO_BUTTON);goButton->setPosition(ccp(tableStartPosition_x+2*tableWidth,tableStartPosition_y-tableHeight*5));addChild(goButton);}//當退出是調用該函數,釋放canPassGrid佔用的空間void GameBaseScene::onExit(){CC_SAFE_DELETE(canPassGrid);Layer::onExit();}


下面看一下initTiledGrid()方法的實現,由於每個關卡地圖大小不一樣,所以這個方法的實現放到了子類中

 

SeaScene.cpp實現如下

SeaScene實現父類GameBaseScene的initTiledGrid方法void GameBaseScene::initTiledGrid(){tiledColsCount = 20;//地圖總的列數tiledRowsCount = 15;//地圖總的行數//根據行列數建立二維數組canPassGridcanPassGrid = new  bool*[tiledRowsCount];   for(int i=0;i<tiledRowsCount;i++)               canPassGrid[i]=new bool [tiledColsCount]; //給canPassGrid設定預設值為false 表示不能通過for(int row = 0;row<tiledRowsCount;row++){for(int col = 0;col<tiledColsCount;col++){canPassGrid[row][col] = false;}}log(""); }


Ok 至此我們已經可以調試一下代碼,從log中可以看到已經列印出了canPassGrid數組中可以通過的行列值了

對比一下sea.tmx檔案中的way圖層 ,看看行列值是不是一致了



點擊下載代碼

http://download.csdn.net/detail/lideguo1979/8276135


擷取路徑稍微複雜一些,我們分多章介紹


未完待續

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.