开发者

cocos2d everlasting growing path

In my app, I need to draw a path where every coupe of frames, an additional point is added to the end of it.

I could implement this开发者_高级运维 in the following way:

- (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;
  }
}

But I'm afraid this will not scale well as the path could (and almost always would) get pretty long.

Is there a better more "economical" way to implement this?


Look at the standard cocos2d RenderTextureTest sample code which includes a fingerpainting class. A simplified version of the main method that does the drawing is shown below. You could take this logic and use it to just render the path under your control rather than driven by the touch events.

-(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];   
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜