Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw smooth circle in iOS sprite kit

I try to draw a single circle in my iOS SpriteKit project but the edges of the circle are not smooth. I would like to have a nicely drawn circle as I would draw it with Photoshop (anti-aliasing). I found several similar questions but the my problem remains.

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        self.backgroundColor = [SKColor whiteColor];
        self.scaleMode = SKSceneScaleModeAspectFill;
        self.size = CGSizeMake(640, 1136);

        CGRect circle = CGRectMake(100.0, 100.0, 80.0, 80.0);
        SKShapeNode *shapeNode = [[SKShapeNode alloc] init];
        shapeNode.path = [UIBezierPath bezierPathWithOvalInRect:circle].CGPath;
        shapeNode.fillColor = [SKColor redColor];
        shapeNode.lineWidth = 0;
        [self addChild:shapeNode];
    }
    return self;
}

What is the trick here?

like image 957
xpepermint Avatar asked Jun 06 '14 09:06

xpepermint


Video Answer


2 Answers

The antialiasing is activated by default, but It only applies to the lines, not the fill.

Set the lineWidth to a number greater than 0 and try again.

like image 169
gabuh Avatar answered Oct 19 '22 01:10

gabuh


Set the antialised property to YES.

shapeNode.antialiased = YES;

The rough edges are not visible when you use fill color.

//shapeNode.fillColor = [SKColor redColor];
shapeNode.strokeColor =[SKColor redColor];
shapeNode.antialiased = YES;
shapeNode.lineWidth = 1;

Now switch between YES and NO for antialisaed property. You will notice the difference.

like image 36
dev Avatar answered Oct 19 '22 00:10

dev