1

我正在寻找一种GKAgent在屏幕上从左到右移动 a 的方法。代理连接到 aGKEntity并且我创建了另外两个代理,称为“leftWall”和“rightWall”。对于每个代理,都有一个匹配的具有唯一名称的精灵节点。我尝试SKPhysicsContact在我的主程序GameScene中使用将一个新的目标代理(例如,如果它与 leftNode 联系,则传递 rightNode 的名称)给在屏幕上移动的代理。

func didBeginContact(contact: SKPhysicsContact) {
        let nodeA = contact.bodyA.node
        let nodeB = contact.bodyB.node

        if nodeA!.name == "leftWall" {
            entityManager.contactWithWall("rightWall", nodeName: (nodeB?.name)!)
        }

为了找到移动代理,我有一个类,其中包含一组实体。

class EntityManager {
    var entities = Set<GKEntity>()
.
.
.
func contactWithWall (newWallName: String, nodeName: String) {
        for entity in entities {
            if let spriteComponent = entity.componentForClass(SpriteComponent.self) {
                if spriteComponent.node.name == nodeName 
                    entity.targetWallEntityName = newWallName
                }
            }
        }
    }

}

从移动GKAgent中,我查找对应的墙的代理targetWallEntityName(这是一个字符串变量)并将其传递给“seek GKGoal”。这基本上是可行的,除了当我设置多个代理时,传递一个新targetWallEntityName的不仅会影响与左墙或右墙接触的代理,还会影响所有代理。结果是所有代理都在屏幕中振荡,响应最靠近两堵墙的代理的物理接触。任何想法如何解决这个问题?我应该先尝试将目标传递给实体,然后再将其发送给它的代理吗?

4

1 回答 1

0

谢谢0x141E。我已经将问题追溯到 one 的缩放比例spriteNode与其对应的physicsBody. 使用SKPhysics触发一个新的代理来寻找工作正常。但是由于某种原因,当一个节点过于频繁地触发碰撞时(在这种情况下是因为它的身体大小),游戏中的所有代理似乎都从这个代理那里接管了寻找目标,并且游戏进入了混乱状态。

于 2015-12-26T10:20:10.607 回答