小羊驼和你一起学习cocos2d-x之五(结合摇杆控制玩家走动)

来源:互联网 发布:ns为含源线性电阻网络 编辑:程序博客网 时间:2024/04/27 17:39

欢迎转载:请保留原文出处

http://blog.csdn.net/linyongliang?viewmode=list


摇杆代码请看摇杆那部分 不过要把

CCPoint getDirection();       
float getVelocity();  

弄在public处

角度是笛卡尔坐标系。左下角为原点。角度0到2 PI。



void LayerPlay::updatePlayer(float dt){float velocity=rocker->getVelocity();if (velocity<2)return;CCPoint point=rocker->getDirection();float angle=atan2f(-point.y,-point.x);turnToAngle(anglePlayer,angle,0.15f);spritePlayer->setRotation(-anglePlayer*180/M_PI);point=ccp(velocity*cos(anglePlayer),velocity*sin(anglePlayer));velocity*=0.2f;float x=velocity*cos(anglePlayer);float y=velocity*sin(anglePlayer);if ((pointPlayer.x+x<1280-120)&&(pointPlayer.x+x>120)){pointPlayer.x+=x;}if ((pointPlayer.y+y<720-120)&&(pointPlayer.y+y>120)){pointPlayer.y+=y;}//pointPlayer=ccpAdd(pointPlayer,point);spritePlayer->setPosition(ccp(pointPlayer.x*ratioWidth,pointPlayer.y*ratioHeight));}

static void getSmallAngle(float& angle){while(angle>=2*M_PI){angle=angle-2*M_PI;}while(angle<0){angle=angle+2*M_PI;}}static void turnToAngle(float& fromAngle,float& toAngle,float range){getSmallAngle(fromAngle);getSmallAngle(toAngle);float temp=fromAngle-toAngle;if ((temp>-2*M_PI+range&&temp<=-M_PI)||(temp>range&&temp<=M_PI)){fromAngle-=range;}else if ((temp>-M_PI&&temp<-range)||(temp>M_PI&&temp<2*M_PI-range)){fromAngle+=range;}}



其中ratioElement  ratioWidth ratioHeight  是缩放变量可以不管。这个是通过摇杆来获取信息来调整玩家的位子和角度

其中屏幕分辨率是1280*720 的 最多不能走出距离边缘120像素地方

原创粉丝点击