【问题标题】:How to get Hexagon shape from SKPhysicsBody from SpriteKit framework如何从 SpriteKit 框架中的 SKPhysicsBody 获取六边形形状
【发布时间】:2017-10-05 12:14:03
【问题描述】:

我正在快速开发 iOS 应用程序。我想展示像 Apple Music 气泡这样的布局。下面的代码可以很好地显示该布局,但是,我想显示 Hexagon 布局而不是圆形气泡。

谁能建议/指导我,如何显示六边形布局而不是圆形布局。

在磁性类中:

 func configure() {

         physicsWorld.gravity = CGVector(dx: 0, dy: 0)
         physicsBody = SKPhysicsBody(edgeLoopFrom: { () -> CGRect in
             var frame = self.frame
             frame.size.width = CGFloat(magneticField.minimumRadius)
             frame.origin.x -= frame.size.width / 2
             return frame
         }())
         let strength = Float(max(size.width, size.height))
         let radius = strength.squareRoot() * 100
         magneticField.region = SKRegion(radius: radius)
         magneticField.minimumRadius = radius
         magneticField.strength = strength
         magneticField.position = CGPoint(x: size.width / 2, y: size.height / 2)
     }

在节点类中:

public init(text: String?, image: UIImage?, color: UIColor, radius: CGFloat) {
    super.init(circleOfRadius: radius)

    self.physicsBody = {
        let body = SKPhysicsBody(circleOfRadius: radius + 2)
        body.allowsRotation = false
        body.friction = 0
        body.linearDamping = 3
        return body
    }()
    self.fillColor = .white
    self.strokeColor = .white
    _ = self.sprite
    _ = self.text
    configure(text: text, image: image, color: color)
}

【问题讨论】:

  • 你的代码对我们没有任何帮助,你只展示了一个物理体和一个磁场
  • @Knight0fDragon 我也添加了节点类代码,你能检查一下,并提供任何建议来获得六边形形状吗?谢谢

标签: ios iphone swift xcode sprite-kit


【解决方案1】:

扩展 KnightofDragon 的答案,您可以使用以下伪代码/数学计算六边形的 6 个点:

PI =  3.14159
for i in 0..<6 {
    theta = i*2.0*PI / n; // angle to the ith vertex, in radians (not degrees)
    x = radius * cos(theta)
    y = radius * sin(theta)
    vertex = centerPosition.x + x, centerPosition.y + y 
}

【讨论】:

  • 你可以做 Float.pi 而不必担心自己的常量,+1 的帮助
  • 谢谢。我从两周前做的一个 C++/openGL 项目中删除了它,并且承认并没有花太多精力将它变成 Swift。
  • 我试图这样做,就像下面这样 let lineWidth: CGFloat = 2 let hexaGonPath = roundedPolygonPath(rect: rectFrame, lineWidth: lineWidth, side: 6,cornerRadius: 10, rotationOffset: CGFloat(M_PI / 2.0)) 一样! CGPath 但是,它越来越糟糕
【解决方案2】:

您需要做的就是使用不同的初始化程序。您正在使用 super.init(circleOfRadius: radius) 制作六边形,只需使用 super.init(path path:CGPath) 其中路径是六边形形状的 CGPath。 (CGPath 很容易理解,用 6 个点创建一个六边形,不管你需要多大)然后你可以走这条路,通过 let body = SKPhysicsBody(polygonFrom:path) 把它交给物理体。

就个人而言,您可能希望减少使用闭包的方式。 (let physicsBody = {...}()) 它们不能保证立即触发,并可能导致并发问题。纠正它的更好方法是在 if let 语句中进行:

if let body = SKPhysicsBody(circleOfRadius: radius + 2)
{        
    body.allowsRotation = false
    body.friction = 0
    body.linearDamping = 3
    self.physicsBody = body
}

【讨论】:

  • 顺便说一句:立即调用的闭包与并发无关,并且确实是立即调用的。 let foo = {...}()let foo = someFunc() 具有相同的效果,只是不需要在其他地方定义someFunc 的主体。
  • @rickster,我将不得不检查我的一些代码,但我相信我在后台线程上执行此类操作时遇到了问题,导致我的闭包崩溃,因为闭包是预期的一个不再存在的对象
  • 我试图这样做,就像下面这样 let lineWidth: CGFloat = 2 let hexaGonPath = roundedPolygonPath(rect: rectFrame, lineWidth: lineWidth, side: 6,cornerRadius: 10, rotationOffset: CGFloat(M_PI / 2.0)) 一样! CGPath 但是,它越来越糟糕
  • 什么是roundedPolygonPath?它没有返回有效的 CGPath
猜你喜欢
  • 2013-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多