0

In my App the Pink heart want to go to the red one! But as you see the way is blocked.
The code I use is:

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]

But the Application always crashes because there is no way!
If I want to find a path from the pink heart to the yellow/green one the code works.
I tried many different ways to solve the problem but nothing seems to work...
e.g.:

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")
}

or

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!
} 

I tried many more possible solutions, but it always crashed...
Thank you for your help!
Thats the image:

1 Answers1

0

You can do it this Way:

    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
        }
    }