在cocos2d图层上绘制直线或矩形

发布于 2024-10-23 16:15:18 字数 1281 浏览 4 评论 0原文

你能让我知道使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箜明 2024-10-30 16:15:18

您可以创建一个单独的图层并像这样调用绘制方法:

-(void) draw
{
    CGSize s = [[Director sharedDirector] winSize];

    drawCircle( ccp(s.width/2,  s.height/2), circleSize, 0, 50, NO);

它是针对圆形的,但原理是相同的。这是我不久前做的一个项目,当时它就有效了。不知道从那以后有没有什么改变。

You could create a separate layer and call the draw method like this:

-(void) draw
{
    CGSize s = [[Director sharedDirector] winSize];

    drawCircle( ccp(s.width/2,  s.height/2), circleSize, 0, 50, NO);

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.

吹泡泡o 2024-10-30 16:15:18

您需要向图层添加绘制方法:

-(void) draw {
    // ...
}

在其中您可以使用一些类似 openGL 的函数和 openGL 的 cocos2d 包装方法。

提示:在draw方法中还可以调用其他方法。
但请记住,使用其他名称的方法
包含openGL指令,上面提到的不调用内部绘制根本行不通。
即使从 update 方法或 ScheduleUpdate 选择器使用的其他方法调用也是如此。

所以你最终会得到这样的结果:

-(void) draw {
    glEnable(GL_LINE_SMOOTH);
    glColor4ub(255, 0, 100, 255);
    glLineWidth(4);
    CGPoint verts[] = { ccp(0,200), ccp(300,200) };
    ccDrawLine(verts[0], verts[1]);

    [self drawSomething];
    [self drawSomeOtherStuffFrom:ccp(a,b) to:ccp(c,d)];

    [someObject doSomeDrawingAsWell];
}

有关更多信息,请查看 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:

-(void) draw {
    // ...
}

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:

-(void) draw {
    glEnable(GL_LINE_SMOOTH);
    glColor4ub(255, 0, 100, 255);
    glLineWidth(4);
    CGPoint verts[] = { ccp(0,200), ccp(300,200) };
    ccDrawLine(verts[0], verts[1]);

    [self drawSomething];
    [self drawSomeOtherStuffFrom:ccp(a,b) to:ccp(c,d)];

    [someObject doSomeDrawingAsWell];
}

For more information check out cocos2d-iphone programming guide :

http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:draw_update?s[]=schedule#draw

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文