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
- position == (-10051366.0, 251672512.0)
- position == (1368370.0, 259904576.0)
- position == (-131583.0, 264841120.0)
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:)