绘制 cocos2d v3 不再有 ccDraw* 功能,不制作节点如何绘制?节点、功能、不再有、cocos2d

2023-09-06 09:17:15 作者:雨落心尘

我按照 http://www.cocos2d-swift.org/get-started 上的说明进行操作

并通过 Sprite Builder 创建了一个新项目.

and made a new Project through Sprite Builder.

搜索 ccDraw 没有显示任何内容.我在论坛上找到了这个例子,并实现了,但它看起来不正确.我不想要一个对资源征税的drawNode.我想要 ccDrawLine 过去如何工作的低级 GL 绘图线.当我做这样的 drawNode 时——它不会重置旧的绘图——所以我画的所有线条都留在屏幕上.

searching for ccDraw doesn't show anything. I found this example on a forum, and implemented, but it doesn't look right. I don't want a drawNode that taxes resources. I want low level GL drawing line how ccDrawLine used to work. When I do a drawNode like this -- it doesn't reset the old drawing -- so all lines I draw stay on the screen.

如何像在 v2.x 中一样进行绘制?(ccDrawLine, ccDrawCircle, ccDrawPoly)

How do I draw like in v2.x? (ccDrawLine, ccDrawCircle, ccDrawPoly)

#import "MainScene.h"

@implementation MainScene

- (id)init {
    self = [super init];
    _line01 = [CCDrawNode node];
    [self addChild:_line01];
    [self schedule:@selector(pulse) interval:0.016];
    return self;
}

- (void)draw:(CCRenderer *)renderer transform:(const GLKMatrix4 *)transform {
    [_line01 drawSegmentFrom:ccp(50, 50) to:ccp(x, y) radius:2 color:[CCColor colorWithRed:128 green:25/255 blue:3]];
}

- (void)pulse {
    x+= 1;
    y+= 3;
    if (x > 500) {
        x = 0;
    } else if (y > 500) {
        y = 0;
    }
}

@end

http://www.cocos2d-swift.org/docs/api/Classes/CCDrawNode.html 建议使用 CCDrawNode 效率不高.

http://www.cocos2d-swift.org/docs/api/Classes/CCDrawNode.html suggests using a CCDrawNode is not efficient.

推荐答案

简单修改pulse如下,去掉draw覆盖.

simply modify pulse slightly as follows, and get rid of the draw override.

- (void)pulse {
    x+= 1;
    y+= 3;
    if (x > 500) {
        x = 0;
    } else if (y > 500) {
        y = 0;
    }
    // update the line segment
    [_line01 clear];
    [_line01 drawSegmentFrom:ccp(50, 50) to:ccp(x, y) radius:2 color:[CCColor colorWithRed:128 green:25/255 blue:3]];

}