Cocos2d iphone cocos2d永恒成长之路
在我的应用程序中,我需要绘制一条路径,在该路径中,每两帧的末尾都会添加一个附加点 我可以通过以下方式实现这一点:Cocos2d iphone cocos2d永恒成长之路,cocos2d-iphone,Cocos2d Iphone,在我的应用程序中,我需要绘制一条路径,在该路径中,每两帧的末尾都会添加一个附加点 我可以通过以下方式实现这一点: - (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 (
- (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;
}
}
但我担心这不会像路径可能(而且几乎总是)变得相当长那样具有很好的伸缩性。有没有更好的更“经济”的方法来实现这一点?看看标准的cocos2d RenderTextureTest示例代码,其中包括FingerPaint类。绘制图形的主要方法的简化版本如下所示。您可以采用此逻辑,并使用它仅渲染您控制下的路径,而不是由触摸事件驱动
-(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];
}
-(void)CCTouchsMoved:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
CGPoint start=[触摸位置视图:[触摸视图]];
start=[[CCDirector sharedDirector]convertToGL:start];
CGPoint end=[touch previousLocationInView:[touch view]];
end=[[CCDirector sharedDirector]convertToGL:end];
//开始绘制渲染纹理
[目标开始];
//对于额外的点,我们将从最后一个位置平滑地绘制此图,并改变精灵的颜色
//缩放/旋转/偏移
浮动距离=距离(起点、终点);
如果(距离>1)
{
int d=(int)距离;
对于(int i=0;i
查看标准cocos2d RenderTexture重新测试示例代码,其中包括FingerPaint类。绘制图形的主要方法的简化版本如下所示。您可以采用此逻辑,并使用它仅渲染您控制下的路径,而不是由触摸事件驱动
-(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];
}
-(void)CCTouchsMoved:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
CGPoint start=[触摸位置视图:[触摸视图]];
start=[[CCDirector sharedDirector]convertToGL:start];
CGPoint end=[touch previousLocationInView:[touch view]];
end=[[CCDirector sharedDirector]convertToGL:end];
//开始绘制渲染纹理
[目标开始];
//对于额外的点,我们将从最后一个位置平滑地绘制此图,并改变精灵的颜色
//缩放/旋转/偏移
浮动距离=距离(起点、终点);
如果(距离>1)
{
int d=(int)距离;
对于(int i=0;i