我目前在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'时没问题。 –