SKSpriteNode - create a round corner node? SKSpriteNode - create a round corner node? ios ios

SKSpriteNode - create a round corner node?


To get a rounded corner node you can use 2 approaches, each of them requires use of SKShapeNode.

First way is to use SKShapeNode and set its path to be a rounded rectangle like this:

SKShapeNode* tile = [SKShapeNode node];[tile setPath:CGPathCreateWithRoundedRect(CGRectMake(-15, -15, 30, 30), 4, 4, nil)];tile.strokeColor = tile.fillColor = [UIColor colorWithRed:0.0/255.0                                                    green:128.0/255.0                                                     blue:255.0/255.0                                                    alpha:1.0];

The other one uses sprite node,crop node and SKShapeNode with rounded rectangle as crop nodes mask:

SKSpriteNode *tile = [SKSpriteNode spriteNodeWithColor:[UIColor   colorWithRed:0.0/255.0                                                                           green:128.0/255.0                                                                            blue:255.0/255.0                                                                           alpha:1.0] size:CGSizeMake(30, 30)];SKCropNode* cropNode = [SKCropNode node];SKShapeNode* mask = [SKShapeNode node];[mask setPath:CGPathCreateWithRoundedRect(CGRectMake(-15, -15, 30, 30), 4, 4, nil)];[mask setFillColor:[SKColor whiteColor]];[cropNode setMaskNode:mask];[cropNode addChild:tile];

If your tiles are one solid colour, i suggest you go with the first approach.


In Swift 3 you can create with:

let tile = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 30, height: 30), cornerRadius: 10)


Hope this helps:

SKSpriteNode *make_rounded_rectangle(UIColor *color, CGSize size, float radius){    UIGraphicsBeginImageContext(size);    [color setFill];    CGRect rect = CGRectMake(0, 0, size.width, size.height);    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius];    [path fill];    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    SKTexture *texture = [SKTexture textureWithImage:image];    return [SKSpriteNode spriteNodeWithTexture:texture];}