Search code examples
objective-cmacosquartz-graphicssprite-kit

CGContextAddArc: how to create circle?


I started working on my Tower defense project in Xcode with the new Spritekit framework and I wanted to add a visible radius (circle) around ANY tower that the player would create.

So I created the following code:

// Place Towers
-(void)mouseUp:(NSEvent *)theEvent {
    /* Called when a mouse click occurs */

    CGPoint location = [theEvent locationInNode:self];

    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"tower"];

    sprite.position = location;
    sprite.scale = 1.5;

    CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
    CGContextAddArc(myContext, location.x, location.y, 10, 0, M_PI*2, YES);
    CGContextSetRGBStrokeColor(myContext, 0, 0, 225, 0);

    [self addChild:sprite];

}

I'm not familiar with the angles and other things because I am doing this for the first time, so I'm just guessing that it should be like that. I'm pretty sure that I missed something.

Thanks for any advice (I await some great criticism).


Solution

  • Directly from the SpriteKit programming guide :

    SKShapeNode *ball = [[SKShapeNode alloc] init];
    
    CGMutablePathRef myPath = CGPathCreateMutable();
    CGPathAddArc(myPath, NULL, 0,0, 15, 0, M_PI*2, YES);
    ball.path = myPath;
    
    ball.lineWidth = 1.0;
    ball.fillColor = [SKColor blueColor];
    ball.strokeColor = [SKColor whiteColor];
    ball.glowWidth = 0.5;