我正在寻找一种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
的不仅会影响与左墙或右墙接触的代理,还会影响所有代理。结果是所有代理都在屏幕中振荡,响应最靠近两堵墙的代理的物理接触。任何想法如何解决这个问题?我应该先尝试将目标传递给实体,然后再将其发送给它的代理吗?