Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, December 9, 2016

SKSpriteNode - create a round corner node?

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!

Answer by Thorsten for SKSpriteNode - create a round corner node?


from the class reference:

"An SKSpriteNode is a node that draws a textured image, a colored square, or a textured image blended with a color."

It seems the easiest way is to draw a block with rounded corners and then use one of these class methods:

  • spriteNodeWithImageNamed:
  • spriteNodeWithTexture:
  • spriteNodeWithTexture:size:

Answer by Dobroćudni Tapir for 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.

Answer by Krzysztof Gabis for SKSpriteNode - create a round corner node?


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];  }  

Answer by Thomas Johannesmeyer for SKSpriteNode - create a round corner node?


This is heavily inspired by the accepted answer, yet it uses a more readable way to create the SKShapeNode and also fixes the annoying pixel line around the crop. Seems like a minor detail, but it may cost someone a few minutes.

CGFloat cornerRadius = 15;  SKCropNode *cropNode = [SKCropNode node];  SKShapeNode *maskNode = [SKShapeNode shapeNodeWithRectOfSize:scoreNode.size cornerRadius:cornerRadius];  [maskNode setLineWidth:0.0];  [maskNode setFillColor:[UIColor whiteColor]];  [cropNode setMaskNode:maskNode];  [cropNode addChild:scoreNode];  

Answer by Zoltan Vinkler for SKSpriteNode - create a round corner node?


In Swift 3 you can create with:

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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.