2017-01-09 98 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

似乎內部用於合併單個形狀節點內的多個多邊形的混合模式是「相減」的,因此重疊區域沒有顏色。這是我能找到的最佳解決方法。至少這樣,我們可以將複合形狀作爲一個單獨的實體來操縱。 –