您好,登錄后才能下訂單哦!
這一篇講npc的運動算法,講一下思路,npc有很多的時候,實現很自然且離散的遠動,還是有點難度的。
利用離散函數生成隨機數他t,作為落地點的運動時間,使不同的npc的運動錯開。
利用兩函數實現:
void Global::monsterMove(ccTime dt) { //移除部分后id對應出現換亂,待解決-------- //獲得移除的id號,取過來進行校正 for (int i=0;i<enemyArray->count();i++) { Enemy* monster=enemyArray->getObjectAtIndex(i); float mX=monster->getMonsterSprite()->getPosition().x; float mY=monster->getMonsterSprite()->getPosition().y; float bX=boy->getHeroSprite()->getPosition().x; float bY=boy->getHeroSprite()->getPosition().y; //Coaster Rider Monster Action AI; //use rand(); if(mX>bX) { mXvel=-0.25; //printf("monster is right!should to left!\n"); monster->getMonsterSprite()->setFlipX(false); } else if(bX>mX) { mXvel=0.25; //printf("monster is left!should to right!\n"); monster->getMonsterSprite()->setFlipX(true); } if(monster->getDirection()==MOVERLOOK) { monstersMoving(monster,i); } else if(monster->getDirection()==MATTACK&&monster- >getMonsterSprite()->getPosition().y>5) { monstersMoving(monster,i); } //monstersMoving(monster,i); CCPoint pos=monster->getMonsterSprite()->getPosition(); monster->getMonsterSprite()->setPosition(ccp(pos.x+mXvel,pos.y)); //當***時monster停止跳動over } for (int i=0;i<enemypDingArray->count();i++) { Enemy* monster=enemypDingArray->getObjectAtIndex(i); float mX=monster->getMonsterSprite()->getPosition().x; float mY=monster->getMonsterSprite()->getPosition().y; float bX=boy->getHeroSprite()->getPosition().x; float bY=boy->getHeroSprite()->getPosition().y; //Coaster Rider Monster Action AI; //use rand(); if(mX>bX) { mXvel=-0.25; //printf("monster is right!should to left!\n"); monster->getMonsterSprite()->setFlipX(false); } else if(bX>mX) { mXvel=0.25; //printf("monster is left!should to right!\n"); monster->getMonsterSprite()->setFlipX(true); } if(monster->getDirection()==MOVERLOOK) { monstersMoving(monster,50+i); } else if(monster->getDirection()==MATTACK&&monster->getMonsterSprite()->getPosition().y>5) { monstersMoving(monster,50+i); } //monstersMoving(monster,i); CCPoint pos=monster->getMonsterSprite()->getPosition(); monster->getMonsterSprite()->setPosition(ccp(pos.x+mXvel,pos.y)); //當***時monster停止跳動over } } void Global::monstersMoving(Enemy* monster,int id) { //利用時間錯開的方式,實現enemy差異化運動 //運動還需優化-------------------------------2013.3.2 if(monsterData->getActionTime(id)<=51&&monsterData->getActionTime(id)>=0) { monPos[id].y=(velY[id])*(monsterData->getActionTime(id))-0.5*(GRAVITY*(monsterData->getActionTime(id)))*(monsterData->getActionTime(id)); monPos[id].x=(velX[id])*(monsterData->getActionTime(id)); } float vec2Y=monPos[id].y; float vec2X=monPos[id].x; if(monster->getMonsterSprite()->getPosition().y<5) { monsterPosition[id].y=monster->getMonsterSprite()->getPosition().y; if(velY[id]==0) { monsterPosition[id].y+=0.5*GRAVITY*monsterData->getActionTime(id)*monsterData->getActionTime(id); } if(velY[id]>0) { //monsterData->setActionTime(0.0,id); } int N = 80 + rand() % 120; if(velY[id]>0&&monsterData->getActionTime(id)>=N) { monsterData->setActionTime(0.0,id); } velY[id]=0.0; isMonsterJ[id]=false; isPlayMonsAnim=true; } //有bug,運動不正常,動作要合理的切換 if(monsterData->getActionTime(id)<=51&&monsterData->getActionTime(id)>=0) { monster->getMonsterSprite()->setPosition(ccp(monster->getMonsterSprite()->getPosition().x,monsterPosition[id].y+monPos[id].y)); } float py=monster->getMonsterSprite()->getPosition().y; //正常的寫法 velY[id]=2.5; monsterData->setActionTime(monsterData->getActionTime(id)+0.75,id); //CCLOG("Time:%f y:%f",monsterData->getActionTime(1),py); }
下一篇將講碰撞的處理,應為要用到解析plist文件,大家先看一下下面的博文,
Cocos2d-x游戲開發之TecturePacker的plist解析
http://lonag.blog.51cto.com/3340984/1002265
long原創
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。