【问题标题】:Prevent GKGridGraph crashing while pathfinding防止 GKGridGraph 在寻路时崩溃
【发布时间】:2018-09-02 14:56:05
【问题描述】:

在我的应用程序中,粉红色的心想去红色的!但正如你所见,路被挡住了。
我使用的代码是:

let solution = GridGraph.findPath(from: GridGraph.node(atGridPosition:int2(Int32(pinkHeart.positon.x),Int32(pinkHeart.positon.y)))!,to:GridGraph.node(a[enter image description here][1]tGridPosition:int2(Int32(redHeart.positon.x),Int32(redHeart.positon.y)))!) as! [GKGridGraphNode]

但应用程序总是崩溃,因为没有办法!
如果我想找到一条从粉红色的心到黄色/绿色的路径,那么代码就可以工作。
我尝试了许多不同的方法来解决问题,但似乎没有任何效果...
例如:

do {
         let solution = try GridGraph.findPath(from:GridGraph.node(atGridPosition:int2(Int32(pinkHeart.positon.x),Int32(pinkHeart.positon.y)))!, to:GridGraph.node(atGridPosition: int2(Int32(redHeart.positon.x),Int32(redHeart.positon.y)))!) as! [GKGridGraphNode]
} catch {
         print("no path found")
}

if let solution = GridGraph.findPath(from:GridGraph.node(atGridPosition:int2(Int32(pinkHeart.positon.x),Int32(pinkHeart.positon.y)))!, to:GridGraph.node(atGridPosition: int2(Int32(redHeart.positon.x),Int32(redHeart.positon.y)))!) as! [GKGridGraphNode] {
    //Do something!
} 

我尝试了更多可能的解决方案,但总是崩溃...
感谢您的帮助!
Thats the image:

【问题讨论】:

  • 这需要清理,很难阅读,而且我们不知道粉红心和红心在做什么,因为我们看不到您的应用。
  • 很抱歉之前没有显示爱心...我添加了一张图片来展示案例...

标签: swift sprite-kit gameplay-kit


【解决方案1】:

你可以这样做:

    var solutionPath: [GKGridGraphNode]? {
        let solution = GridGraph.findPath(from: OneNode, to: anotherNode) as! [GKGridGraphNode]
        if solution.isEmpty {
            print("There is no possible path!")
            return nil
        }
        else {
            return solution
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    相关资源
    最近更新 更多