在cocos2d图层上绘制直线或矩形
你能让我知道使用 Cocos2d ios4 iPhone 在场景图层上绘制线条或矩形的最佳方法是什么吗?
到目前为止已经尝试过Texture2d,但它更像是一个画笔,不太好。尝试使用绘制方法绘制一条线,但绘制另一条线时前一条线消失。
基本上是想画多条水平、垂直、斜梁。请建议。任何代码都会有很大帮助。
使用纹理绘制的代码如下:
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];
渲染效果不好,尤其是。使用斜线,因为缩放会影响质量。
干杯
Can you let me know what is the best way to draw a line or rectangle on a scene layer using Cocos2d ios4 iphone.
So far have tried Texture2d, but it is more like a paint brush and is not so good. Tried drawing a line using draw method, but previous line disappears on drawing another line.
Basically want to draw multiple horizontal ,vertical, oblique beams. Please suggest. Any code would help a lot .
The code to draw using texture is below:
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];
The rendering is not good esp. with oblique lines as the scaling affects the quality.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个单独的图层并像这样调用绘制方法:
它是针对圆形的,但原理是相同的。这是我不久前做的一个项目,当时它就有效了。不知道从那以后有没有什么改变。
You could create a separate layer and call the draw method like this:
It's for a circle but the principle is the same. This is from a project I made a while back and it worked then. Don't know if anything has changed since.
您需要向图层添加绘制方法:
在其中您可以使用一些类似 openGL 的函数和 openGL 的 cocos2d 包装方法。
提示:在draw方法中还可以调用其他方法。
但请记住,使用其他名称的方法
包含openGL指令,上面提到的不调用内部绘制根本行不通。
即使从 update 方法或 ScheduleUpdate 选择器使用的其他方法调用也是如此。
所以你最终会得到这样的结果:
有关更多信息,请查看 cocos2d-iphone 编程指南:
http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:draw_update?s[]=schedule#draw
You need to add draw method to your layer:
Inside it you can use some openGL like functions and cocos2d wrapper methods for openGL.
Hint: other methods can be called inside draw method.
But keep in mind that using other name for method
containing openGL instructions, that's not called inside draw mentioned above simply won't work.
Even when called from update method or other method used by scheduleUpdate selector.
So you will end up with something like this:
For more information check out cocos2d-iphone programming guide :
http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:draw_update?s[]=schedule#draw