2017-06-14 43 views
2

我想用spift在sprite工具包中繪製網格拓撲結構(圖形)。 enter image description here我是新來的精靈套件。請給出任何建議或示例代碼。如何使用swift在Sprit工具包中繪製網格拓撲結構

+0

我正在爲你做一個粗略的回答,但實際的'drawLines()'算法我不知道。我假設你知道在地形中繪製線條的算法?我剛剛做的一個非常基礎和不完善。 – Fluidity

+0

嘿,那麼,我的答案+項目解決方案爲你工作?如果沒有,請讓我知道,以便我可以嘗試更新它。 – Fluidity

回答

1

所以正如我在評論中提到的,我不知道如何製作完美的地形算法,但是我確實想出了一些可以複製圖片的東西。

基本上你會添加一堆作爲SKNodes的圖,然後使用.position屬性作爲用CGPath繪製的線的起點和終點。從這條路上,你可以創建一個SKShapeNode(path: CGPath)

我還在這裏添加了一個使用委託的自定義按鈕,但它與地形的實際「內臟」完全分離。這只是一個按鈕。

// Overly complex way of creating a custom button in SpriteKit: 
protocol DrawLinesDelegate: class { func drawLines() } 

// Clickable UI element that will draw our lines: 
class DrawLinesButton: SKLabelNode { 

    weak var drawLinesDelegate: DrawLinesDelegate? 

    init(text: String, drawLinesDelegate: DrawLinesDelegate) { 
    super.init(fontNamed: "Chalkduster") 
    self.drawLinesDelegate = drawLinesDelegate 
    self.text = text 
    isUserInteractionEnabled = true 
    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    print(drawLinesDelegate) 

    drawLinesDelegate?.drawLines() 
    } 
    required init?(coder aDecoder: NSCoder) { fatalError("") } 
    override init() { super.init() } 
}; 


class GameScene: SKScene, DrawLinesDelegate { 

    var plots = [SKShapeNode]() 

    // var lines = [SKShapeNode]() // This may be useful in a better algorithm. 

    var nodesDrawnFrom = [SKShapeNode]() 

    var drawLinesButton: DrawLinesButton? 

    func drawLine(from p1: CGPoint, to p2: CGPoint) { 

    let linePath = CGMutablePath() 
    linePath.move(to: p1) 
    linePath.addLine(to: p2) 

    let line = SKShapeNode(path: linePath) 
    line.strokeColor = .red 
    line.lineWidth = 5 
    // lines.append(line) // Again, may be useful in a better algo. 
    addChild(line) 
    } 

    func drawLines() { 

    // Remove all lines: // Again again, may be useful in a better algorithm. 
    /* 
    for line in lines { 
    line.removeFromParent() 
    lines = [] 
    } 
    */ 

    // The plot that we will draw from: 
    var indexNode = SKShapeNode() 

    // Find indexNode then draw from it: 
    for plot in plots { 

     // Find a new node to draw from (the indexNode): 
     if nodesDrawnFrom.contains(plot) { 
     continue 
     } else { 
     indexNode = plot 
     } 

     // Draw lines to every other node (from the indexNode): 
     for plot in plots { 
     if plot === indexNode { 
      continue 
     } else { 
      drawLine(from: indexNode.position, to: plot.position) 
      nodesDrawnFrom.append(indexNode) 
     } 
     } 
    } 
    } 

    func addNode(at location: CGPoint) { 
    let plot = SKShapeNode(circleOfRadius: 50) 
    plot.name = String(describing: UUID().uuid) 
    plot.zPosition += 1 
    plot.position = location 
    plot.fillColor = .blue 

    plots.append(plot) 
    addChild(plot) 
    } 

    override func didMove(to view: SKView) { 
    drawLinesButton = DrawLinesButton(text: "Draw Lines", drawLinesDelegate: self) 
    drawLinesButton!.position.y = frame.minY + (drawLinesButton!.frame.size.height/2) 
    addChild(drawLinesButton!) 
    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let location = touches.first!.location(in: self) 
    addNode(at: location) 
    } 
} 

enter image description here

不完善算法中:

在這裏你可以看到有多個線路從一個到另一個時,你有中點被抽(即東西擋住直行):enter image description here

您需要在算法中添加另一整個部分來檢查這一點。

另一個需要注意的重要的事情是SKShapeNode()是非常不友好的,最好將所有這些轉換爲SpriteNodes,或者將整個場景轉換爲靜態紋理。

但是,將它們全部作爲ShapeNodes給予您最大的靈活性,並且在這裏最容易解釋。

+0

感謝您的寶貴回覆。在你的代碼中,當我點擊屏幕時,只有我可以繪製節點。我正在尋找具有10個節點並連接到特定節點的完全通用,平均負載拓撲。我仍然在尋找解決方案。 –