我一直在寻找关于这个新框架的日子,并试图利用它的一些功能, 但是......有一些不适合我的东西,demobot源代码在某些方面没有帮助, 我想念一些简单的教程,但这是我的主要疑虑:
let obstacles = scene["obstacle"]
polygonObstacles = SKNode.obstaclesFromNodePhysicsBodies(obstacles)
graph = GKObstacleGraph(obstacles: polygonObstacles, bufferRadius: 60.0)
func drawGraph() {
for node in graph.nodes as! [GKGraphNode2D] {
for destination in node.connectedNodes as! [GKGraphNode2D] {
let points = [CGPoint(node.position), CGPoint(destination.position)]
let shapeNode = SKShapeNode(points: UnsafeMutablePointer<CGPoint>(points), count: 2)
shapeNode.strokeColor = SKColor(white: 1.0, alpha: 0.5)
shapeNode.lineWidth = 5.0
shapeNode.zPosition = 3
scene.addChild(shapeNode)
}
}
}
所以,当我尝试绘制此图并查看连接时,我得到了这个:http://i.imgur.com/EZ3dx5v.jpg 我发现它真的很奇怪,在我放置障碍的任何地方,即使数量很少,屏幕的左角部分总是有更多的连接(半径不会影响它)
我没有在游戏中使用GKComponents,但我试图运行一些GKAgents2D来追捕玩家,如下所示:
func calculateBehaviorForAgents(){
let mainCharacterPosition = float2(scene.mainCharacter.position)
let mainCharacterGraphNode = GKGraphNode2D(point: mainCharacterPosition)
graph.connectNodeUsingObstacles(mainCharacterGraphNode)
for i in 0...monsters.count-1{
let monster = monsters[i]
let agent = agents[i]
let behavior = GKBehavior()
let monsterPosition = float2(monster.position)
let monsterGraphNode = GKGraphNode2D(point: monsterPosition)
graph.connectNodeUsingObstacles(monsterGraphNode)
let pathNodes = graph.findPathFromNode(monsterGraphNode, toNode: mainCharacterGraphNode) as! [GKGraphNode2D]
let path = GKPath(graphNodes: pathNodes, radius: 00.0)
let followPathGoal = GKGoal(toFollowPath: path, maxPredictionTime: 1.0, forward: true)
behavior.setWeight(1.0, forGoal: followPathGoal)
let stayOnPathGoal = GKGoal(toStayOnPath: path, maxPredictionTime: 1.0)
behavior.setWeight(1.0, forGoal: stayOnPathGoal)
agent.behavior = behavior
graph.removeNodes([monsterGraphNode])
}
graph.removeNodes([mainCharacterGraphNode])
}
现在当我调用updateWithDeltaTime方法时,他的委托方法: func agentWillUpdate(agent:GKAgent){} func agentDidUpdate(agent:GKAgent){} 给我代理人意想不到的价值,它的位置没有任何意义,巨大的数字导致战场之外
但是我看到他的速度矢量是有意义的,所以我将它与我的怪物相匹配并将代理更新为怪物的位置
func updateWithDeltaTime(currentTime : CFTimeInterval){
for i in 0...monsters.count-1{
let agent = agents[i]
let monster = monsters[i]
monster.physicsBody?.velocity = CGVectorMake(CGFloat(agent.velocity.x), CGFloat(agent.velocity.y))
agent.updateWithDeltaTime(currentTime)
agent.position = float2(monster.position)
monster.gameSceneUpdate(currentTime)
}
现在我得到了一些结果,但它远离我想要的东西: 怪物没有跟随角色到屏幕的边缘或右上角,我从图表中删除了他们的点数,但是之后为他们创建了一条路径(图像没有这个点,但它们存在)。 显然因为没有通往那里的路径,请记住图像吗?
问题是:如何让这个代理系统工作?
也许我对代理商,目标乃至图表的使用完全错误!我阅读了文档,但我仍然无法做到正确 和更多... 起初,这个怪物并没有避开障碍物,即使GKGoals像是&#34; avoidObstacles&#34;,通过相同的PolygonObstacles, 但是当我改变时
graph.connectNodeUsingObstacles(mainCharacterGraphNode)
graph.connectNodeUsingObstacles(monsterGraphNode)
到
graph.connectNodeUsingObstacles(mainCharacterGraphNode, ignoringObstacles: polygonObstacles)
graph.connectNodeUsingObstacles(monsterGraphNode, ignoringObstacles: polygonObstacles)
它工作了! o.O
我真的需要一些帮助,谢谢大家:D!