2011-09-06 7 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

fingerpainting 클래스를 포함하는 표준 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]; 
} 
관련 문제