Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SKSpriteNode - create a round corner node?

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!

like image 647
Haris Hussain Avatar asked Feb 11 '14 07:02

Haris Hussain


People also ask

How do I round corners of view in Xcode?

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.

How do you round the corners of a view SwiftUI?

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.


1 Answers

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.

like image 69
Dobroćudni Tapir Avatar answered Sep 24 '22 07:09

Dobroćudni Tapir