我想用spift在sprite工具包中繪製網格拓撲結構(圖形)。 我是新來的精靈套件。請給出任何建議或示例代碼。如何使用swift在Sprit工具包中繪製網格拓撲結構
2
A
回答
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)
}
}
不完善算法中:
在這裏你可以看到有多個線路從一個到另一個時,你有中點被抽(即東西擋住直行):
您需要在算法中添加另一整個部分來檢查這一點。
另一個需要注意的重要的事情是SKShapeNode()是非常不友好的,最好將所有這些轉換爲SpriteNodes,或者將整個場景轉換爲靜態紋理。
但是,將它們全部作爲ShapeNodes給予您最大的靈活性,並且在這裏最容易解釋。
+0
感謝您的寶貴回覆。在你的代碼中,當我點擊屏幕時,只有我可以繪製節點。我正在尋找具有10個節點並連接到特定節點的完全通用,平均負載拓撲。我仍然在尋找解決方案。 –
相關問題
- 1. 繪製電網網絡拓撲結構
- 2. 拓撲繪圖工具
- 3. 網絡拓撲結構
- 4. 風暴 - 拓撲結構到拓撲結構
- 5. 如何在風暴拓撲結構中使用drools
- 6. HornetQ集羣拓撲結構
- 7. 殺死拓撲名稱中包含特殊字符的Storm拓撲結構
- 8. CUDA網格的高效拓撲數據結構?
- 9. 使用面向公衆的SharePoint網站的拓撲結構
- 10. Web應用程序中的常見網絡拓撲結構
- 11. 如何動態地在Java中繪製(星形)拓撲?
- 12. 如何調用DAG拓撲重構?
- 13. netbeans - 如何在Netbeans中關閉Apache Storm的拓撲結構?
- 14. NMS期望繪製拓撲的OID
- 15. 如何設置全網格拓撲的Git源代碼控制?
- 16. 如何在Java中動態顯示仿真網絡拓撲結構
- 17. 統一星形拓撲結構
- 18. hadoop羣集的拓撲結構?
- 19. storm-deploy提交拓撲結構java.lang.NoClassDefFoundError
- 20. 計算拓撲結構中的理論網絡帶寬
- 21. 如何使用IDE在風暴生產集羣中提交拓撲結構
- 22. 拓撲繪圖工具與jQuery和Java的
- 23. 如何開始構建Storm的拓撲結構?
- 24. 具有負載均衡和冗餘的RavenDb拓撲結構
- 25. jQuery的:具有相同拓撲結構,但不同的行爲
- 26. 藍牙低功耗廣告數據包和拓撲結構
- 27. 如何使用OpenNMS完成拓撲
- 28. 如何使用Javascript創建拓撲圖
- 29. 神經網絡拓撲
- 30. 網絡拓撲上的Hadoop
我正在爲你做一個粗略的回答,但實際的'drawLines()'算法我不知道。我假設你知道在地形中繪製線條的算法?我剛剛做的一個非常基礎和不完善。 – Fluidity
嘿,那麼,我的答案+項目解決方案爲你工作?如果沒有,請讓我知道,以便我可以嘗試更新它。 – Fluidity