【问题标题】:How to show GKPath in GameplayKit如何在 GameplayKit 中显示 GKPath
【发布时间】:2016-02-07 22:09:04
【问题描述】:

是否有显示 GKPath 的技巧?

例如我的路径:

class ScenarioPath: GKPath {

    //SET
    //   |  |  |  |  |  |
    // --A--B--C--D--E--F--
    //   |  |  |  |  |  |
    //6 punti con raggio quasi 600 e lato 500 va bene per scenario 2K * 3K
    static let lato = Float(500.0)
    static let maxY = Float(0.0)
    static let radius : Float = 600.0

    static let maxYNormalize = maxY - radius

    static let pts = [
        vector_float2(-lato,maxYNormalize),
        vector_float2(+lato,maxYNormalize),
        vector_float2(-2*lato,maxYNormalize),
        vector_float2(+2*lato,maxYNormalize),
        vector_float2(-3*lato,maxYNormalize),
        vector_float2(+3*lato,maxYNormalize)]


    init () {
        super.init(points: UnsafeMutablePointer(ScenarioPath.pts), count: ScenarioPath.pts.count, radius: ScenarioPath.radius, cyclical: true)
    }


}

我正在寻找一个简单的函数来显示这条路径。 谢谢

【问题讨论】:

    标签: ios swift sprite-kit gameplay-kit


    【解决方案1】:

    GameplayKit 不是图形框架。这是一个可以与任何图形框架一起使用的低级工具包。它没有任何用于绘制GKPaths 的功能,因为解释路径的方式至少与图形工具包和坐标系一样多。

    由于您正在使用 SpriteKit,因此使用与您的 GKPath 相同的一组点来制作 CGPath 并不难,然后将其分配给 SKShapeNode 以放置在您的场景中。

    您可以在他们的两个较大的演示项目中找到 Apple 示例代码:AgentsCatalogDemoBots

    这里有一些帮助您入门:

    // BTW, vector_float2 just a typealias for float2,
    // and is ArrayLiteralConvertible, so you can write your array shorter:
    let pts: [float2] = [
        [-lato,maxYNormalize],
        [+lato,maxYNormalize],
        // ...
    ]
    
    // CGPoint extension to make the conversion more readable and reusable
    extension CGPoint {
        init(_ vector: float2) {
            self.init(x: CGFloat(vector.x), y: CGFloat(vector.y))
        }
    }
    
    // make array of CGPoints from array of float2
    let cgPoints = pts.map(CGPoint.init)
    // use that array to create a shape node
    let node = SKShapeNode(points: UnsafeMutablePointer(cgPoints), count: cgPoints.count)
    

    当然,这假设您的路径是在与场景相同的坐标系中指定的。如果没有,您将需要一些转换。

    【讨论】:

      猜你喜欢
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      相关资源
      最近更新 更多