SKSpriteNode - 创建一个圆角节点? [英] SKSpriteNode - create a round corner node?

查看:796
本文介绍了SKSpriteNode - 创建一个圆角节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让SKSpriteNode圆形走投无路?我正在尝试用颜色填充的SKSpriteNode创建一个Tile likesqaure块:

  SKSpriteNode * tile = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithRed: 0.0 / 255.0 
绿色:128.0 / 255.0
蓝色:255.0 / 255.0
alpha:1.0]大小:CGSizeMake(30,30)];

我怎样才能让它成为一个角落?



谢谢!

解决方案

要获得圆角节点,您可以使用2种方法,每种方法都需要使用SKShapeNode。 / p>

第一种方法是使用SKShapeNode并将其路径设置为圆角矩形,如下所示:

  SKShapeNode * tile = [SKShapeNode节点]; 
[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];

另一个使用精灵节点,裁剪节点和带圆角矩形的SKShapeNode作为裁剪节点掩码:

  SKSpriteNode * tile = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithRed:0.0 / 255.0 
green:128.0 / 255.0
蓝色:255.0 / 255.0
alpha:1.0]大小:CGSizeMake(30,30)];
SKCropNode * cropNode = [SKCropNode节点];
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];

如果您的瓷砖是纯色,我建议您选择第一种方法。


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!

解决方案

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.

这篇关于SKSpriteNode - 创建一个圆角节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆