cocosd-x 虚拟摇杆

来源:互联网 发布:广东数据库程序设计 编辑:程序博客网 时间:2024/05/11 23:31

转自:http://blog.csdn.net/xiaoxiangp/article/details/7689719

这个只是点我是读了  老G的博客,其中有一个游戏例子就是<是男人就坚持20秒>这标题引人遐想啊~

下面是老G的博客,里面有很多不错的例子...大家多关注啦

 “老G的小屋” 博客,请务必保留此出处http://4137613.blog.51cto.com/4127613/757254



图中左下角为摇杆,本例子中用于控制小人这张图片的移动



我是直接修改HelloWorld例子,所以代码分几个步骤:


1.在HelloWorld类中添加需要的函数和成员变量,代码如下:

[cpp] view plaincopy
  1. using namespace cocos2d;  
  2.   
  3. class HelloWorld : public CCLayer  
  4. {  
  5. public:  
  6.     virtual bool init();  
  7.     static cocos2d::CCScene* scene();  
  8.   
  9.     //触摸相关的处理函数  
  10.     virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);  
  11.     virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);  
  12.     virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);  
  13.   
  14.     void moveing(ccTime dt);//移动逻辑  
  15.   
  16. private:  
  17.     CCPoint centre;  
  18.     CCSprite *yg2;//摇杆小圆圈,在类中创建是因为其他函数还会用到,不像背景只需要初始化加载就行  
  19.     float radius;  
  20.   
  21.     //精灵相关  
  22.     CCSprite * jl;  
  23.     float speedX;  
  24.     float speedY;  
  25.     bool isMove;  
  26.   
  27.     LAYER_NODE_FUNC(HelloWorld);  
  28. };  

代码中有许多陌生的类型(类即类型)这个就需要大家多看,不看几次是不会懂的,给出一个连接,官方的API结构说明,虽然是英文的,用翻译软件理解的大概也不错,以后用多了自然就懂了

http://www.cocos2d-x.org/embedded/cocos2d-x/classes.html


2.实现初始化函数...代码如下:


[cpp] view plaincopy
  1. bool HelloWorld::init()  
  2. {  
  3.     bool bRet = false;  
  4.     do   
  5.     {  
  6.         CC_BREAK_IF(! CCLayer::init());  
  7.         CCSize s = CCDirector::sharedDirector()->getWinSize();//获得屏幕大小,保持到s中  
  8.   
  9.         CCSprite *yg1 = CCSprite::spriteWithFile("yg1.png");//加载摇杆背景图  
  10.         yg1->setOpacity(190);                        //设置透明度  
  11.         yg1->setAnchorPoint(ccp(0,0));               //设置锚点位置  
  12.         yg1->setPosition(ccp(0,0));                  //设置显示位置  
  13.         radius = yg1->getContentSize().width/2;      //计算半径  
  14.         centre = ccp(radius,radius);                //计算中心  
  15.         this->addChild(yg1,1);                       //添加到场景中  
  16.   
  17.         yg2 = CCSprite::spriteWithFile("yg2.png");//加载摇杆小圈  
  18.         yg2->setPosition(ccp(centre.x, centre.y));  
  19.         this->addChild(yg2,2);  
  20.   
  21.         jl = CCSprite::spriteWithFile("jl.png");//加载需要被控制的图片...叫精灵好听一些  
  22.         CC_BREAK_IF(!jl);  
  23.         jl->setPosition(ccp(s.width/2, s.height/2));  
  24.         this->addChild(jl,1);  
  25.   
  26.   
  27.         this->setIsTouchEnabled(true);               //开启触摸,不开的话点击屏幕是没用的  
  28.         this->schedule(schedule_selector(HelloWorld::moveing));//这句理解为开启逻辑吧,这里的逻辑是处理精灵的移动  
  29.         bRet = true;  
  30.     } while (0);  
  31.   
  32.     return bRet;  
  33. }  

3.实现触摸逻辑:


[cpp] view plaincopy
  1. //触摸开始..也就是点击屏幕  
  2. void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)  
  3. {  
  4.     CCTouch *touch = (CCTouch*)pTouches->anyObject();  
  5.     CCPoint location = touch->locationInView(touch->view());  
  6.     CCPoint convertedLocation = CCDirector::sharedDirector()->convertToGL(location);  
  7.   
  8.     CCRect rect=yg2->boundingBox();  
  9.     if (CCRect::CCRectContainsPoint(rect,convertedLocation))//判断触摸的范围。如果是在摇杆内的话,才响应  
  10.     {  
  11.         isMove=true;  
  12.     }  
  13. }  
  14.   
  15. //触摸移动的处理...也就是按下后再移动  
  16. void HelloWorld::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)  
  17. {  
  18.     CCTouch *touch = (CCTouch*)pTouches->anyObject();  
  19.     CCPoint location = touch->locationInView(touch->view());  
  20.     CCPoint convertedLocation = CCDirector::sharedDirector()->convertToGL(location);  
  21.   
  22.     bool inRange=pow(centre.x-convertedLocation.x,2)+pow(centre.y-convertedLocation.y,2)<pow(radius,2);  
  23.   
  24.     if(isMove&&inRange)  
  25.     {  
  26.         CCPoint position=jl->getPosition();  
  27.         yg2->setPosition(convertedLocation);  
  28.   
  29.         float radius1=radius*2/6;//十字键中心区的内切圆半径  
  30.         float side=radius*2/3;//九宫格中一个格子的边长  
  31.   
  32.         //我们使用海伦公式来计算面积,进而判断十字键中心区的精确方向  
  33.         //向上  
  34.         if(triangleContainPoint(centre.x,centre.y,centre.x-radius1,centre.y+radius1,centre.x+radius1,centre.y+radius1,convertedLocation.x,convertedLocation.y)  
  35.             ||CCRect::CCRectContainsPoint(CCRectMake(centre.x-radius1,centre.y+radius1,side,side),convertedLocation))  
  36.         {  
  37.             speedX=0;  
  38.             speedY=1;  
  39.         }  
  40.         //向下  
  41.         else if(triangleContainPoint(centre.x,centre.y,centre.x-radius1,centre.y-radius1,centre.x+radius1,centre.y-radius1,convertedLocation.x,convertedLocation.y)  
  42.             ||CCRect::CCRectContainsPoint(CCRectMake(centre.x-radius1,centre.y-radius1-side,side,side),convertedLocation))  
  43.         {  
  44.             speedX=0;  
  45.             speedY=-1;  
  46.         }  
  47.         //向左  
  48.         else if(triangleContainPoint(centre.x,centre.y,centre.x-radius1,centre.y+radius1,centre.x-radius1,centre.y-radius1,convertedLocation.x,convertedLocation.y)  
  49.             ||CCRect::CCRectContainsPoint(CCRectMake(centre.x-radius1-side,centre.y-radius1,side,side),convertedLocation))  
  50.         {  
  51.             speedX=-1;  
  52.             speedY=0;  
  53.         }  
  54.         //向右  
  55.         else if (triangleContainPoint(centre.x,centre.y,centre.x+radius1,centre.y+radius1,centre.x+radius1,centre.y-radius1,convertedLocation.x,convertedLocation.y)  
  56.             ||CCRect::CCRectContainsPoint(CCRectMake(centre.x+radius1+side,centre.y-radius1,side,side),convertedLocation))  
  57.         {  
  58.             speedX=1;  
  59.             speedY=0;  
  60.         }  
  61.         //右上  
  62.         else if(convertedLocation.x-centre.x>0&&convertedLocation.y-centre.y>0)  
  63.         {  
  64.             speedX=0.7f;  
  65.             speedY=0.7f;  
  66.         }  
  67.         //左上  
  68.         else if (convertedLocation.x-centre.x<0&&convertedLocation.y-centre.y>0)  
  69.         {  
  70.             speedX=-0.7f;  
  71.             speedY=0.7f;  
  72.         }  
  73.         //左下  
  74.         else if (convertedLocation.x-centre.x<0&&convertedLocation.y-centre.y<0)  
  75.         {  
  76.             speedX=-0.7f;  
  77.             speedY=-0.7f;  
  78.         }  
  79.         //右下  
  80.         else if (convertedLocation.x-centre.x>0&&convertedLocation.y-centre.y<0)  
  81.         {  
  82.             speedX=0.7f;  
  83.             speedY=-0.7f;  
  84.         }  
  85.     }  
  86. }  
  87.   
  88. //触摸结束..也就是松开后的处理  
  89. void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)  
  90. {  
  91.     isMove=false;            //关闭移动逻辑  
  92.     yg2->setPosition(centre);//把摇杆小圈初始化为中心  
  93.     speedX=speedY=0;  
  94. }  

4.触摸中使用的海伦公式用到了两个函数,得自己写上,记得放在触摸处理之前,或是声明一下,不然找不到函数:


[cpp] view plaincopy
  1. float heronsformula(float x1,float y1,float x2,float y2,float x3,float y3)  
  2. {  
  3.     float a=sqrt(pow(x1-x2,2)+pow(y1-y2,2));  
  4.     float b=sqrt(pow(x2-x3,2)+pow(y2-y3,2));  
  5.     float c=sqrt(pow(x3-x1,2)+pow(y3-y1,2));  
  6.     float s=(a+b+c)/2;  
  7.   
  8.     return sqrt(s*(s-a)*(s-b)*(s-c));  
  9. }  
  10.   
  11. bool triangleContainPoint(float x1,float y1,float x2,float y2,float x3,float y3,float px,float py)  
  12. {  
  13.     float s1=heronsformula(x1,y1,x2,y2,px,py);  
  14.     float s2=heronsformula(x2,y2,x3,y3,px,py);  
  15.     float s3=heronsformula(x3,y3,x1,y1,px,py);  
  16.     float s=heronsformula(x1,y1,x2,y2,x3,y3);  
  17.   
  18.     return abs(s-(s1+s2+s3))<0.001f;  
  19. }  

5.完成精灵的移动逻辑函数


[cpp] view plaincopy
  1. //移动逻辑的处理函数  
  2. void HelloWorld::moveing(ccTime dt)  
  3. {  
  4.     if (isMove&&(speedX!=0||speedY!=0))  
  5.     {  
  6.         CCPoint position=ccp(jl->getPosition().x+speedX,jl->getPosition().y+speedY);  
  7.   
  8.         CCSize size=CCDirector::sharedDirector()->getWinSize();  
  9.         CCRect rect=CCRectMake(0,0,size.width,size.height); //创建一个逻辑矩形,范围是屏幕的大小,用于判断边界  
  10.   
  11.         if(CCRect::CCRectContainsPoint(rect,position))  
  12.         {  
  13.             jl->setPosition(position);  
  14.         }  
  15.     }  
  16. }  



这是用到的图片...如果格式有问题还需要自己转换一下..