swift - 调用costToNode : in subclassed GKGraphNode2D with GameplayKit

标签 swift xcode sprite-kit path-finding gameplay-kit

情况

我正在使用 GKObstacleGraph 在以下 map 上进行寻路: enter image description here

橙色区域是障碍物 (GKPolygonObstacle),而紫色点和线是自定义放置的节点 (GKGraphNode2D) 及其各自的连接,实体可以使用它们来传送穿过障碍物。

一切正常,实体找到了正确的路径: enter image description here

目标

我认为在方法上正确的是将紫色节点之间的成本设置为 0(因为它们是门户)。考虑到这一点,我首先将所有紫色节点子类为 DoorGraphNodes:

import SpriteKit
import GameplayKit

class DoorGraphNode: GraphNode {

    var id: String!
    var floor: Int!
    var complement: DoorGraphNode!

    init(position: CGPoint, id: String, floor: Int) {
        self.id = id
        self.floor = floor
        let point = vector_float2(Float(position.x), Float(position.y))
        super.init(point: point)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setComplement(doorNode: DoorGraphNode) {
        self.complement = doorNode
    }
}

然后,我将要添加到 GKObstaclesGraph 的所有 GKGraphNode2D 节点子类化为 GraphNode:

import SpriteKit
import GameplayKit

class GraphNode: GKGraphNode2D {

    override func cost(to node: GKGraphNode) -> Float {
        if let fromNode = self as? DoorGraphNode {
            if let toNode = node as? DoorGraphNode {
                if fromNode.complement == toNode {
                    return 0.0
                }
            }
        }
        return self.cost(to: node)
    }
}

这样,当我调用障碍图.findPath(from: startNode, to: targetNode) 时,GameplayKit 理论上应该在两个连接的(互补)紫色 (DoorGraphNode) 节点之间调用 costToNode: 方法时接收到 0.0。

问题

但实际上,我收到 EXC_BAD_ACCESS 错误,控制台中没有详细信息: enter image description here

我不认为我在逻辑上做错了什么。您知道为什么会发生这种情况吗?

最佳答案

GraphNode 类中的第 22 行正在调用自身。它应该调用它的父级。

return super.cost(to: node)

关于swift - 调用costToNode : in subclassed GKGraphNode2D with GameplayKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41506737/

相关文章:

ios - 如何更改 SWIFT 中节点下降的速度

ios - Xcode 6.3 : 8. 0 或 8.0.2 模拟器

ios - Q : Spritekit SKPhysics

ios - UITableView 在 segmentedControlChanged 上没有改变

objective-c - Cocoa 始终以权限运行应用程序

c - swift 等同于 C 中的头文件?

c++ - 如何在 Xcode 6.1.1 中使用 google test 运行本地 c++ 单元测试

ios - iOS 13 应用程序的 "Enable Multiple Windows"设置(仅限 iPhone)

ios - UITableViewController 中的 customCell UITextField 在运行时 swift 不显示

swift - Swift 中的 MD5 实现