这一篇讲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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。