SKSpriteNode - 创建一个圆角节点? [英] SKSpriteNode - create a round corner node?
问题描述
有没有办法让 SKSpriteNode 圆角?我正在尝试使用填充颜色的 SKSpriteNode 创建一个 Tile likesqaure 块:
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?
谢谢!
推荐答案
要获得圆角节点,您可以使用 2 种方法,每种方法都需要使用 SKShapeNode.
To get a rounded corner node you can use 2 approaches, each of them requires use of SKShapeNode.
第一种方法是使用 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];
另一种使用精灵节点,裁剪节点和带有圆角矩形的SKShapeNode作为裁剪节点掩码:
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屋!