Cocos2d creates a circle shape and uses it as a sprite

so I would like to create a circle shape with primitives on cocos2d and then use it as a sprite, how can I do this? I know that I need to use something like this:

glLineWidth(16); glColor4ub(0, 255, 0, 255); drawCircle( ccp(s.width/2, s.height/2), 100, 0, 10, NO); But it's hard for me to understand how it works and how to use it as a sprite

+3
source share
1 answer

Do you really need an instance of CCSprite? you can subclass CCNode, then in

- (void) draw

enter your code here. your circle will have a central position (0.f, 0.f)

@implementation MyScene

- (void) onEnter
{
    [super onEnter];
    CCNode* myNode = [MyNodeSubclass node];
    [node setPosition: someRandomPosition ];
    [self addChild: node];
}

@end

@implementation MyNodeSubclass

- (void) draw
{
    glColor4f(255, 255, 255, 255);
    CCPoint center = ccp(0.f, 0.f);
    CGFloat radius = 10.f;
    CGFloat angle = 0.f;
    NSInteger segments = 10;
    BOOL drawLineToCenter = YES;

    ccDrawCircle(center, radius, angle, segments, drawLineToCenter);    
}

@end

wrote this piece of code right here, did not copy it from xcode, but it should work the way you want. ccDrawCircle is a cocos2d function declared in CCDrawingPrimitives.h

+4
source

All Articles