Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

objective c - drawing cocos2d v3 no longer has ccDraw* functions, how do I draw without making nodes?

I followed the instructions on http://www.cocos2d-swift.org/get-started

and made a new Project through Sprite Builder.

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.

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 suggests using a CCDrawNode is not efficient.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]];

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...