Is there a way to make a SKSpriteNode round cornered? I am trying to create a Tile likesqaure blocks with color filled SKSpriteNode:
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)];
How can I make it round cornered?
Thanks!
You can give it round corners by changing the cornerRadius property of the view's layer . and smaller values give less rounded corners. Both clipsToBounds and masksToBounds are equivalent.
Any SwiftUI view can have its corners rounded using the cornerRadius() modifier. This takes a simple value in points that controls how pronounced the rounding should be.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With