【发布时间】:2016-07-12 06:05:11
【问题描述】:
在线教程提到使用 SpriteKit 场景编辑器创建圆形和其他形状。一个这样的例子是:https://www.raywenderlich.com/118225/introduction-sprite-kit-scene-editor。
但是,对象库中唯一可用的形状节点类型是方形。
您如何使用对象库来创建正方形以外的其他形状?
【问题讨论】:
标签: xcode sprite-kit skscene
在线教程提到使用 SpriteKit 场景编辑器创建圆形和其他形状。一个这样的例子是:https://www.raywenderlich.com/118225/introduction-sprite-kit-scene-editor。
但是,对象库中唯一可用的形状节点类型是方形。
您如何使用对象库来创建正方形以外的其他形状?
【问题讨论】:
标签: xcode sprite-kit skscene
到目前为止,方形是您唯一的选择。也许在下一个版本中,它们将允许更多形状,但截至目前,我会避免使用 SKShapeNode,因为它们非常有问题
来源:XCode 场景编辑器,唯一的选项是正方形,所有选项卡都不允许更改或子类化
【讨论】:
SKShapeNode 对象绘制由 Core Graphics 路径定义的形状。
图形路径是直线和曲线的集合 可以定义开放或封闭的子路径。您可以指定单独的 路径的填充和描边部分的渲染行为。 每个部分都可以使用纯色或纹理进行渲染;如果 你需要渲染更复杂的效果,你也可以使用 自定义着色器。
形状节点对于不易分解的内容很有用 变成简单的纹理精灵。形状节点对于 在游戏之上构建和显示调试信息 内容。但是,SKSpriteNode 类提供了更高的性能 比这个类,所以要谨慎使用形状节点。
从路径创建形状节点显示了如何创建形状节点的示例 形状节点。该示例创建了一个内部为蓝色的圆圈和一个 白色轮廓。创建路径并将其附加到形状节点的 路径属性。
更多详情可以联系苹果官方guide
这是来自来源的实际可用的 init 方法:
/* Create a Shape Node using a CGPathRef, optionally centered at the Node's origin. */
@available(iOS 8.0, *)
public convenience init(path: CGPath)
@available(iOS 8.0, *)
public convenience init(path: CGPath, centered: Bool)
/* Create a Shape Node representing a Rect. */
@available(iOS 8.0, *)
public convenience init(rect: CGRect)
/* Create a Shape Node representing a rect centered at the Node's origin. */
@available(iOS 8.0, *)
public convenience init(rectOfSize size: CGSize)
/* Create a Shape Node representing a rounded rect with a corner radius */
@available(iOS 8.0, *)
public convenience init(rect: CGRect, cornerRadius: CGFloat)
/* Create a Shape Node representing a rounded rect with a corner radius centered at the Node's origin. */
@available(iOS 8.0, *)
public convenience init(rectOfSize size: CGSize, cornerRadius: CGFloat)
/* Create a Shape Node representing an circle centered at the Node's origin. */
@available(iOS 8.0, *)
public convenience init(circleOfRadius radius: CGFloat)
/* Create a Shape Node representing an Ellipse inscribed within a Rect */
@available(iOS 8.0, *)
public convenience init(ellipseInRect rect: CGRect)
/* Create a Shape Node representing an Ellipse inscribed within a Rect centered at the Node's origin. */
@available(iOS 8.0, *)
public convenience init(ellipseOfSize size: CGSize)
/* Create a Shape Node representing an a series of Points interpreted as line segments */
@available(iOS 8.0, *)
public convenience init(points: UnsafeMutablePointer<CGPoint>, count numPoints: Int)
/* Create a Shape Node representing a smoothed spline that passes through a series of Points */
@available(iOS 8.0, *)
public convenience init(splinePoints points: UnsafeMutablePointer<CGPoint>, count numPoints: Int)
如您所见,您不仅可以使用自定义CGPath 创建不规则多边形,还可以创建不规则多边形。
一个典型的例子如下:
let shape = SKShapeNode()
shape.path = UIBezierPath(roundedRect: CGRect(x: -128, y: -128, width: 256, height: 256), cornerRadius: 64).CGPath
shape.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))
shape.fillColor = UIColor.redColor()
shape.strokeColor = UIColor.blueColor()
shape.lineWidth = 10
addChild(shape)
附注:
在开发过程中可能会出现由SKShapeNode引起的一些内存错误,但不会妨碍您测试,创建和发布游戏,市场上的许多游戏都使用SKShapeNode没有问题,您必须更加小心不滥用代码。
【讨论】: