另一种实现 Fruit Ninja 里刀

来源:互联网 发布:ubuntu 配置maven 编辑:程序博客网 时间:2024/04/28 00:35
实现原理: 

  画直线 

    在一个 list 列表里记录所有的触摸点,在 draw 函数里开始画线,线段逐渐加粗,在末端逐渐减细。可以直接用 Cocos2d 里的 box2d 模板,添加了少量代码即可。 

//使用list列表保存所有点 
代码  收藏代码
  1. std::list<CGPoint> pointl;  
  2. -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {  
  3.      
  4.     UITouch *touch = [touches anyObject];  
  5.      
  6.     CGPoint start = [touch locationInView: [touch view]];     
  7.     start = [[CCDirector sharedDirector] convertToGL: start];  
  8.      
  9.     CGPoint end = [touch previousLocationInView:[touch view]];  
  10.     end = [[CCDirector sharedDirector] convertToGL:end];  
  11.      
  12.     float distance = ccpDistance(start, end);  
  13.     if (distance > 1)  
  14.     {  
  15.         int d = (int)distance;  
  16.         for (int i = 0; i < d; i++ )  
  17.         {  
  18.             float difx = end.x - start.x;  
  19.             float dify = end.y - start.y;  
  20.             float delta = (float)i / distance;  
  21.             CGPoint p;  
  22.             p.x = start.x + (difx * delta);  
  23.             p.y = start.y + (dify * delta);  
  24.              
  25.             pointl.push_back(p);  
  26.         }  
  27.     }  
  28.      
  29.     pointcount = pointl.size();  
  30.   
  31. }  
  32.   
  33. //*************************************  
  34. draw函数核心代码  
  35.   
  36. -(void) draw  
  37. {  
  38.     CGPoint pr;  
  39.     glPointSize( 0.3f );  
  40.     list <CGPoint>::iterator b = pointl.begin();  
  41.     glColor4ub(255,255,255,32);  
  42.      
  43.     for(;b!=pointl.end();b++)  
  44.     {  
  45.         CGPoint pt = *b;  
  46.         ps++;  
  47.         //控制线段的粗细,使达到两头细中间粗的效果  
  48.         if (ps > (pl -30 )) // initlw > 5 )  
  49.         {  
  50.            initlw=initlw-lwc;  
  51.         }  
  52.         else  
  53.         {  
  54.           if (initlw < 6 )  
  55.           {  
  56.             initlw =initlw+lwc;  
  57.           }  
  58.         }  
  59.         glLineWidth( initlw);  
  60.         if (pr.x > 1 && pr.y > 1 )  
  61.         {  
  62.             //画线段,也可以使用点  
  63.             ccDrawLine(pr, pt );  
  64.             
  65.         }  
  66.         pr = *b;  
  67.     }  
  68. }  
  69.   
  70. //**********************************************  
  71. //自动缩短线段  
  72.   
  73. -(void) tick: (ccTime) dt  
  74. {  
  75.     //It is recommended that a fixed time step is used with Box2D for stability  
  76.     //of the simulation, however, we are using a variable time step here.  
  77.     //You need to make an informed choice, the following URL is useful  
  78.     //http://gafferongames.com/game-physics/fix-your-timestep/  
  79.      
  80.     int32 velocityIterations = 8;  
  81.     int32 positionIterations = 1;  
  82.      
  83.     // Instruct the world to perform a single step of simulation. It is  
  84.     // generally best to keep the time step and iterations fixed.  
  85.     world->Step(dt, velocityIterations, positionIterations);  
  86.      
  87.     //*********************************************  
  88.     //**  
  89.     for (int i=0; i<12 ; i++)  
  90.     {  
  91.         if (pointl.size() >0)  
  92.         {  
  93.            pointl.pop_front();  
  94.            pointcount--;  
  95.         }  
  96.         else {  
  97.             break;  
  98.         }  
  99.     }  
  100.   
  101.     //为了使线段不过长  
  102.     while (pointcount >200) {  
  103.         pointl.pop_front();  
  104.         //pointcount--;  
  105.         pointcount=pointl.size();  
  106.     }  
  107.     //********************************************  
  108. }