Michal Rogowski
Michal Rogowski

Reputation: 431

GKBehaviour in xcode 8 beta 4 makes gkagent position crazy

So I'm creating a game where I want to add some GKGoal for my GKAgent behaviour.

So after hours of fighting, I've downloaded next project this time is from Apple Agents Catalog, and in Xcode 7.3 it works. I rewrite it to Swift and create basic GKAgent with GKGoal(toWander:) this is my code:

 class AAPLAgentNode: SKNode, GKAgentDelegate {
    init(withScene scene:SKScene ,radius: Float, position:CGPoint) {
        super.init()
        
        self.position = position
        self.zPosition = 10
        scene.addChild(self)
        
        agent = GKAgent2D()
        agent.radius = radius
        agent.position = vector2(Float(position.x), Float(position.y))
        agent.delegate = self
        agent.maxSpeed = 100
        agent.maxAcceleration = 50
        
        let circleShape = SKShapeNode(circleOfRadius: CGFloat(radius))
        circleShape.lineWidth = 2.5
        circleShape.fillColor = SKColor.gray
        circleShape.zPosition = 1
        self.addChild(circleShape)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func agentWillUpdate(_ agent: GKAgent) {
        
    }
    
    func agentDidUpdate(_ agent: GKAgent) {
        self.position = CGPoint(x: Double(self.agent.position.x), y: Double(self.agent.position.y))
        print("aaa == \(self.position)")
    }
    
    var agent: GKAgent2D!
}

When I add to scene

     let wanderer = AAPLAgentNode(withScene: self, radius: 100, position: CGPoint(x: 0, y: 0))
    
    wanderer.agent.behavior = GKBehavior(goal: GKGoal(toWander: 10), weight: 100)

    agentSystem.addComponent(wanderer.agent)

without behaviour position is static but when I add it, position goes crazy and in each iteration of update, values are like

Is it just an Xcode 8 beta bug, or am I doing something wrong. I spend a lot of hours trying to work it out. Thanks:)

Upvotes: 3

Views: 306

Answers (2)

Joe Bentley
Joe Bentley

Reputation: 296

I know this is very old, but are you calling with a very high deltaTime on your first update?

I was calling with the system time as my first update call to the agent behaviour, causing a massive jump at the first timestep

Upvotes: 2

apolwla
apolwla

Reputation: 66

It's been super frustrating trying to grock this Agent, Behaviour stuff... I was having this issue in Xcode 9.0 while trying out toSeekAgent when I was adding my nodes and agents in the didMove() method of my SKScene but after "init-ing" them from touchesBegan() the extreme position issue went away.

Upvotes: 1

Related Questions