在我的应用程序中,我需要绘制一条路径,其中每个框架的coupe都会在它的末尾添加一个附加点。
我可以通过以下方式实现此功能:
- (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;
}
}但我担心这不会很好地扩展,因为这条道路可能(几乎总是会)变得相当长。
有没有更好、更“经济”的方法来实现这一点?
发布于 2012-01-21 16:50:11
看看标准的cocos2d RenderTextureTest示例代码,其中包含一个指纹绘制类。执行绘图的main方法的简化版本如下所示。您可以采用此逻辑,并使用它来呈现您控制下的路径,而不是由触摸事件驱动。
-(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];
}https://stackoverflow.com/questions/7315488
复制相似问题