使用SpriteKit场景编辑器绘制圆圈和其他形状



在线教程暗示使用SpriteKit场景编辑器创建圆形和其他形状。一个这样的例子是:https://www.raywenderlich.com/118225/introduction-sprite-kit-scene-editor。

但是,对象库中唯一可用的形状节点类型是正方形。

如何使用对象库创建除正方形之外的其他形状?

截至目前,square是您唯一的选择。 也许在下一个版本中,它们将允许更多形状,但截至目前,我会避免使用SKShapeNode的,因为它们非常有缺陷

来源:XCode场景编辑器,唯一的选项是方形,没有一个选项卡允许更改或子类化

SKShapeNode 对象绘制由核心图形路径定义的形状。

图形路径是直线和曲线的集合,可以定义开放或封闭子路径。您可以指定单独的路径的填充和描边部分的呈现行为。每个部分都可以使用纯色或纹理进行渲染;如果您需要渲染更复杂的效果,也可以使用自定义着色器。

形状节点对于无法轻松分解的内容非常有用变成简单纹理的精灵。形状节点也非常有用在游戏之上构建和显示调试信息内容。但是,SKSpriteNode类提供了更高的性能。比这个类,所以要谨慎使用形状节点。

从路径创建形状节点显示了如何创建形状节点。该示例创建一个内部为蓝色和白色轮廓。创建路径并将其附加到形状节点的路径属性。

您可以在Apple官方指南中找到更多详细信息

这是源代码中实际可用的 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,您必须在代码中更加小心而不会滥用它。

相关内容

最新更新