I'm looking for a way to move a GKAgent
from left to right across a screen. The agent is connected to a GKEntity
and I've created two further agents, called "leftWall" and "rightWall". For each agent, there is a matching sprite node with a unique name. I've tried to use SKPhysicsContact
in my main GameScene
to pass a new target agent (e.g. if it makes contact with the leftNode, pass the rightNode's name) to the agent moving through the screen.
func didBeginContact(contact: SKPhysicsContact) {
let nodeA = contact.bodyA.node
let nodeB = contact.bodyB.node
if nodeA!.name == "leftWall" {
entityManager.contactWithWall("rightWall", nodeName: (nodeB?.name)!)
}
To get to the moving agent, I have a class in between which holds an array of entities.
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
}
}
}
}
}
From in the moving GKAgent
, I look up the agent for the wall that corresponds targetWallEntityName
(which is a String variable) and pass this to a 'seek GKGoal
.' This basically works except that when I set up a number of agents, passing a new targetWallEntityName
doesn't just affect the agent that has made contact with the left or right wall, it affects all agents. The results is all agents are oscillating in the screen, responding to the physics contact from the agents closest to the two walls. Any ideas how to resolve this? Should I try to pass the target to the entity first, before sending it to it's agent?
Thanks 0x141E
. I've traced the problem to a difference between the scaling of one spriteNode
and its corresponding physicsBody
. Using SKPhysics
to trigger a new agent to seek works fine. For some reason though, when one node triggers a collision too frequently (because of it's body's size in this case), all the agents in the game seem to take over the seek target from this one agent, and the game enters a chaotic state.