Swift 防止GKGridGraph在寻路时崩溃
在我的应用程序中,粉红色的心想要变成红色的心!但正如你所见,道路被封锁了。Swift 防止GKGridGraph在寻路时崩溃,swift,sprite-kit,gameplay-kit,Swift,Sprite Kit,Gameplay Kit,在我的应用程序中,粉红色的心想要变成红色的心!但正如你所见,道路被封锁了。 我使用的代码是: 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(
我使用的代码是:
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]
但是应用程序总是崩溃,因为没有办法如果我想找到一条从粉色心脏到黄色/绿色心脏的路径,那么代码可以工作。
我尝试了许多不同的方法来解决这个问题,但似乎没有任何效果…
e、 g: 或 我尝试了更多可能的解决方案,但总是失败…
谢谢你的帮助
您可以这样做:
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
}
}
这需要清理,很难阅读,我们不知道粉红红心和红色红心在做什么,因为我们看不到你的应用。很抱歉,以前没有显示红心。。。我添加了一张图片来展示这个案例。。。
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!
}
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
}
}