2011-09-06 29 views
1

在我的應用程序中,我需要繪製一個路徑,其中每一個車架,一個額外的點被添加到它的結尾。cocos2d永恆的成長路徑

我可以通過以下方式實現這一點:

- (void) draw 
{ 
    glEnable(GL_LINE_SMOOTH); 
    glColor4f(0.0,0.0,1.0,1.0); 

    BOOL first = YES; 
    CGPoint prevPoint; 

    for (NSValue* v in points) 
    { 
    CGPoint p = [v CGPointValue]; 

    if (first == YES) 
     first = NO; 
    else 
     ccDrawLine(prevPoint, p); 

     prevPoint = p; 
    } 
} 

但是我恐怕這不會作爲路徑可以(而且幾乎總是會)會很長很好地擴展。
有沒有更好的「經濟」方式來實現這一點?

回答

1

看看包含指紋類的標準cocos2d RenderTextureTest示例代碼。下圖顯示了繪製主要方法的簡化版本。你可以採取這個邏輯,並用它來渲染你的控制下的路徑,而不是觸摸事件驅動的路徑。

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint start = [touch locationInView: [touch view]]; 
    start = [[CCDirector sharedDirector] convertToGL: start]; 
    CGPoint end = [touch previousLocationInView:[touch view]]; 
    end = [[CCDirector sharedDirector] convertToGL:end]; 

    // begin drawing to the render texture 
    [target begin]; 

    // for extra points, we'll draw this smoothly from the last position and vary the sprite's 
    // scale/rotation/offset 
    float distance = ccpDistance(start, end); 
    if (distance > 1) 
    { 
     int d = (int)distance; 
     for (int i = 0; i < d; i++) 
     { 
      float difx = end.x - start.x; 
      float dify = end.y - start.y; 
      float delta = (float)i/distance; 
      [brush setPosition:ccp(start.x + (difx * delta), start.y + (dify * delta))]; 
      [brush setRotation:rand()%360]; 
      float r = ((float)(rand()%50)/50.f) + 0.25f; 
      [brush setScale:r]; 
      //[brush setColor:ccc3(CCRANDOM_0_1()*127+128, 255, 255) ]; 
      // Call visit to draw the brush, don't call draw.. 
      [brush visit]; 
     } 
    } 
    // finish drawing and return context back to the screen 
    [target end]; 
}