Cocos2d-x 3.2 大富翁遊戲項目開發-第八部分 角色按路徑行走

路徑獲得之後,我們就可以讓角色按照路徑行走了,當點擊go按鈕的時候,我們調用player的startGo()方法,傳入的參數就是保存了路徑的2個一維數組

void GameBaseScene::goButtonCallback(cocos2d::CCObject *pSender)
{
	RouteNavigation::getInstance()->getPath(player1,3,canPassGrid,tiledRowsCount,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]);
	}
	//調用RicherPlayer類的startGo方法
	player1->startGo(rowVector,colVector);
}


給類RicherPlayer添加相應的startGo方法

void RicherPlayer::startGo(std::vector<int> rowVector,std::vector<int> colVector)
{
	//獲取遊戲控制器RicherGameController,調用其中的startRealGo()方法,開始真正的角色行走
	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]; //獲取第一個位置的行列值
	nextRow =0;	nextCol =0; //下一步的行列值
	//創建上下左右的動作,並放入緩存
	if(!AnimationCache::getInstance()->animationByName("left_animation"))
	{
AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_left_vector(),playerGoPerFrameTime),"left_animation");
	}
	if(!AnimationCache::getInstance()->animationByName("right_animation"))
	{
AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_right_vector(),playerGoPerFrameTime),"right_animation");
	}
	if(!AnimationCache::getInstance()->animationByName("down_animation"))
	{
AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_down_vector(),playerGoPerFrameTime),"down_animation");
	}
	if(!AnimationCache::getInstance()->animationByName("up_animation"))
	{
AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_up_vector(),playerGoPerFrameTime),"up_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 一下,引用計數加一,防止動畫被清除
	 left->retain();
	 right ->retain();
	 down->retain();
	 up->retain();
	//根據參數給相應變量賦值
	_rowVector=rowVector;
	_colVector=colVector;
	_richerPlayer =richerPlayer;
	stepHasGone = 0;//角色已經走了幾步
	stepsCount = _rowVector.size()-1;//取得路徑需要走的步數,因爲第一個是當前位置的行列,所以不計入步數
	moveOneStep();//開始行走,先走一步,走完一步後,再走下一步
}

void RicherGameController::moveOneStep()
{
		//獲取下一步行列,計算同當前行列的差值
		nextRow = _rowVector[stepHasGone+1];	
                nextCol = _colVector[stepHasGone+1];
		int distanceRow = nextRow - currentRow;	
                int distanceCol = nextCol - currentCol;

		MoveBy* moveBy;	Repeat* repeate;	Action* spawnAction;
		//根據行列的差值,創建上下左右相應的動作,包括移動和行走的動畫

		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);
		}
		//創建同步動畫,當移動完畢,執行callEndGoFunc方法,進而調用endGo()方法
		spawnAction = Sequence::create(Spawn::create(moveBy,repeate,NULL),callEndGoFunc,NULL);
		_richerPlayer->runAction(spawnAction);
}

void RicherGameController::endGo()
{
	stepHasGone++;//走完一步後,已走步數加1
	if(stepHasGone >= stepsCount) //如果已走步數大於等於總步數,返回
	{
		return;
	}
	currentRow = nextRow;
	currentCol = nextCol;//當前行列賦值爲下一行列
	moveOneStep();//開始下一步的移動
	log("go end");
}


經過測試發現,角色會來回走動,走過去了還走回來,所以我們需要給角色類Player添加 表示角色從哪個位置過來的屬性

修改RouteNavigation類的getPath()方法

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);
	}
	//設置角色從哪裏來的位置爲false ,以表示不可通過
	canPassGrid_copy[player->getComeFromeRow()][player->getComeFromCol()] = false;
………………………..
	//獲取完路徑後,設置角色的來自的位置
	player->setComeFromCol(pathCols_vector[pathCols_vector.size()-2]);
	player->setComeFromeRow(pathRow_vector[pathRow_vector.size()-2]);
}

測試發現角色終於可以正常走動了




流程圖如下


目前爲止,代碼寫的相對較多了,有必要重新整理一下,下部分,我們進行一下代碼優化。便於後期的繼續開發。


點擊下載代碼   http://download.csdn.net/detail/lideguo1979/8292407

未完待續........................


發佈了77 篇原創文章 · 獲贊 48 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章