【问题标题】:Use PhysicsBody with SKShapenode line. Swift, iOS, SpriteKit使用带有 SKShapenode 线的 PhysicsBody。斯威夫特,iOS,SpriteKit
【发布时间】:2018-06-06 14:58:11
【问题描述】:

我需要使用 ShapeBody 添加简单的线条进行交互。 我正在尝试下面的代码,Xcode 给了我下一个响应:

PhysicsBody:无法创建物理体。

        let testPath = UIBezierPath()
        testPath.move(to: CGPoint(x:-100, y: 200))
        testPath.addLine(to: CGPoint(x:100, y: 200))

        let testShape = SKShapeNode()
        testShape.path = testPath.cgPath
        testShape.position = CGPoint(x:0, y:250)
        testShape.zPosition = 5
        testShape.lineWidth = 5
        testShape.strokeColor = .red
        testShape.physicsBody = SKPhysicsBody(polygonFrom: testPath.cgPath)

【问题讨论】:

    标签: ios swift sprite-kit skphysicsbody skshapenode


    【解决方案1】:

    使用edgeChainFrom 代替了polygonFrom,效果很好!

    testShape.physicsBody = SKPhysicsBody(edgeChainFrom: testPath.cgPath)
    

    【讨论】:

    • 这意味着你的物理体没有体积,也不是动态的
    【解决方案2】:

    路径不能与 SKPhysicsBody 的任何线相交,您的线需要是细长的矩形

        let testPath = UIBezierPath()
        testPath.move(to: CGPoint(x:-100, y: 200))
        testPath.addLine(to: CGPoint(x:100, y: 200))
        testPath.addLine(to: CGPoint(x:100, y: 201))
        testPath.addLine(to: CGPoint(x:-100, y: 201))
        testPath.close()
        let testShape = SKShapeNode()
        testShape.path = testPath.cgPath
        testShape.position = CGPoint(x:0, y:250)
        testShape.zPosition = 5
        testShape.lineWidth = 5
        testShape.strokeColor = .red
        testShape.physicsBody = SKPhysicsBody(polygonFrom: testPath.cgPath)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2017-07-22
      • 2017-03-24
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多