我目前在Xcode 8中工作,使用Swift 3和SpritKit中的新SKTileMapNode來製作二維地牢爬行器類型的遊戲。 我很難讓GKObstacleGraph與地圖一起工作。請幫忙! 我嘗試遍歷tilemap的障礙層內的所有圖塊,併爲每個圖塊創建一個多邊形並將其存儲在GKObstacleGraph中。障礙層中的每個瓦片都是牆面瓦片。地圖看起來像某種類型的地牢爬行器,所以牆壁遍佈各處。 我有類似如下:讓GKObstacleGraph與SKTileMapNode一起工作
for row in 0..<tileMapNode.numberOfRows {
for column in 0..<tileMapNode.numberOfColumns {
let tile = tileMapNode.tileDefinition(atColumn: column, row: row)
let tileCenter = tileMapName.centerOfTile(atColumn: column, row: row)
//find 4 corners of each tile from its center
let bottomLeft = float2(CGPointMake(tileCenter.x - tile.size.width/2, tileCenter.y - tile.size.height/2))
let bottomRight = float2(CGPointMake((tileCenter.x - tile.size.width/2, tileCenter.y + tile.size.height/2))
let topRight = float2(CGPointMake((tileCenter.x + tile.size.width/2, tileCenter.y + tile.size.height/2))
let topLeft = float2(CGPointMake((tileCenter.x - tile.size.width/2, tileCenter.y + tile.size.height/2))
var vertices = [topLeft , bottomLeft , bottomRight , topRight ]
let obstacle = GKPolygonObstacle(points: &vertices, count: 4)
obstacleGraph.add(obstacle)
}
}
然而,當我運行的應用程序它表明,有超過80000個節點,太多的尋路pathes。 任何幫助,將不勝感激。
感謝馬克。我嘗試使用obstacleNodePhysicsBodies函數,但它最終給了我相同的結果,這很有意義,因爲每個牆磚都有自己的物理體,並且地圖上有數百個物體。我想過GKGridGraph,但不知道它是否合適,因爲我的遊戲中的實體在地圖上自由移動,它不是基於網格的遊戲。即使遊戲不是基於網格的,我仍然可以使用GKGridGraph進行尋路? – Bob
還有一件事。我打算使用GKAgent移動敵方實體。有點像:GKGoal(toFollowPath:path,maxPredictionTime:1.0,forward:true)。因此該路徑必須包含GKGraphNodes,而不是GKGridGraphNodes。 – Bob
'GKGridGraphNode'繼承自'GKGraphNode',所以在使用'GKGoal'時沒問題。 –