您好,登錄后才能下訂單哦!
這一篇講npc的運(yùn)動算法,講一下思路,npc有很多的時候,實(shí)現(xiàn)很自然且離散的遠(yuǎn)動,還是有點(diǎn)難度的。
利用離散函數(shù)生成隨機(jī)數(shù)他t,作為落地點(diǎn)的運(yùn)動時間,使不同的npc的運(yùn)動錯開。
利用兩函數(shù)實(shí)現(xiàn):
void Global::monsterMove(ccTime dt) { //移除部分后id對應(yīng)出現(xiàn)換亂,待解決-------- //獲得移除的id號,取過來進(jìn)行校正 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)); //當(dāng)***時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)); //當(dāng)***時monster停止跳動over } } void Global::monstersMoving(Enemy* monster,int id) { //利用時間錯開的方式,實(shí)現(xiàn)enemy差異化運(yùn)動 //運(yùn)動還需優(yōu)化-------------------------------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,運(yùn)動不正常,動作要合理的切換 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); }
下一篇將講碰撞的處理,應(yīng)為要用到解析plist文件,大家先看一下下面的博文,
Cocos2d-x游戲開發(fā)之TecturePacker的plist解析
http://lonag.blog.51cto.com/3340984/1002265
long原創(chuàng)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。