【发布时间】:2011-03-16 23:17:18
【问题描述】:
您能否告诉我使用 Cocos2d ios4 iphone 在场景图层上绘制线条或矩形的最佳方法是什么。
到目前为止已经尝试过Texture2d,但它更像是一个画笔,不是那么好。尝试使用 draw 方法绘制一条线,但前一条线在绘制另一条线时消失了。
基本上想绘制多个水平、垂直、倾斜的光束。请建议。任何代码都会有很大帮助。
使用纹理绘制的代码如下:
CGPoint start = edge.start;
CGPoint end = edge.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 setScale:0.3];
// Call visit to draw the brush, don't call draw..
[brush visit];
}
}
// finish drawing and return context back to the screen
[target end];
渲染效果不好,尤其是。使用斜线,因为缩放会影响质量。
干杯
【问题讨论】:
-
你看过绘制原语的例子吗?
-
可以,但只能画一条线。绘制另一个使其松开前一行。是否应该在 draw 方法中一次重做整个绘图?
-
是的..你需要一个数组来存储线条的所有位置..所以在draw方法中,它循环遍历数组并获取所有要绘制的位置..
标签: iphone cocos2d-iphone line