2017-01-09 100 views
2

我发现,它可能使用单一SKShapeNode对象呈现多个多边形:使用单个SKShapeNode呈现多个重叠的多边形?

class GameScene: SKScene { 

    override func didMove(to view: SKView) { 
     let polygons = [ 
      [ 
       CGPoint(x: 0, y: 0), 
       CGPoint(x: 100, y: 100), 
       CGPoint(x: 100, y: 0) 
      ], 
      [ 
       CGPoint(x: 50, y: 50), 
       CGPoint(x: 50, y: 150), 
       CGPoint(x: 150, y: 150), 
       CGPoint(x: 150, y: 50), 
      ], 
     ] 

     let path = CGMutablePath() 

     for points in polygons { 
      path.addLines(between: points) 
      path.closeSubpath() 
     } 

     let node = SKShapeNode(path: path) 
     node.fillColor = UIColor.red 

     addChild(node) 
    } 
} 

然而,在多边形重叠的任何空间呈现为空:

enter image description here

是否有可能填补那些空白的空间,同时继续使用单个节点?

+1

简短的答案是...你不能。但是,您可以从多边形创建图像,从图像创建纹理,并从纹理创建精灵节点。 – 0x141E

+0

你解决了这个问题吗? – 0x141E

+0

@ 0x141E我尝试了纹理生成方法,但是在每一帧上执行它都太慢了,这是我需要的一个形状转换节点。 – scribu

回答

0

如果你不关心边界,这里是一个kluge解决方法。

class GameScene: SKScene { 

    override func didMove(to view: SKView) { 
     let polygons = [ 
      [ 
       CGPoint(x: 0, y: 0), 
       CGPoint(x: 100, y: 100), 
       CGPoint(x: 100, y: 0) 
      ], 
      [ 
       CGPoint(x: 50, y: 50), 
       CGPoint(x: 50, y: 150), 
       CGPoint(x: 150, y: 150), 
       CGPoint(x: 150, y: 50), 

      ] 
     ] 

     let path = CGMutablePath() 

     for points in polygons { 
      path.addLines(between: points) 
      path.closeSubpath() 
     } 

     let first = CGMutablePath() 
     first.addLines(between: polygons[0]); 
     first.closeSubpath() 

     let second = CGMutablePath() 
     second.addLines(between: polygons[1]); 
     second.closeSubpath() 

     let node = SKShapeNode(path: first) 
     node.fillColor = .red 
     node.strokeColor = .red 

     let child = SKShapeNode(path: second) 
     child.fillColor = .red 
     child.strokeColor = .red 
     node.addChild(child) 


     addChild(node) 
    } 
} 

如果你关心的边界,然后...

class GameScene: SKScene { 

    override func didMove(to view: SKView) { 
     let polygons = [ 
      [ 
       CGPoint(x: 0, y: 0), 
       CGPoint(x: 100, y: 100), 
       CGPoint(x: 100, y: 0) 
      ], 
      [ 
       CGPoint(x: 50, y: 50), 
       CGPoint(x: 50, y: 150), 
       CGPoint(x: 150, y: 150), 
       CGPoint(x: 150, y: 50), 

      ] 
     ] 

     let path = CGMutablePath() 

     for points in polygons { 
      path.addLines(between: points) 
      path.closeSubpath() 
     } 

     let first = CGMutablePath() 
     first.addLines(between: polygons[0]); 
     first.closeSubpath() 

     let second = CGMutablePath() 
     second.addLines(between: polygons[1]); 
     second.closeSubpath() 

     let node = SKShapeNode(path: first) 
     node.fillColor = .red 
     node.strokeColor = .white 
     node.lineWidth = 2 

     let child = SKShapeNode(path: second) 
     child.fillColor = .red 
     child.strokeColor = .white 
     node.lineWidth = 2 
     node.addChild(child) 

     let child2 = SKShapeNode(path: first) 
     child2.fillColor = .red 
     child2.strokeColor = .clear 
     node.lineWidth = 2 
     node.addChild(child2) 


     addChild(node) 
    } 
} 
+0

Erm ...这不符合主要约束 - 它使用多个SKShapeNode对象。 – scribu

+0

似乎内部用于合并单个形状节点内的多个多边形的混合模式是“相减”的,因此重叠区域没有颜色。这是我能找到的最佳解决方法。至少这样,我们可以将复合形状作为一个单独的实体来操纵。 –