所以我正在创建一个游戏,我想GKGoal
为我的GKAgent
行为添加一些内容。
所以经过几个小时的战斗,我这次从 Apple Agents Catalog下载了下一个项目,并且在 Xcode 7.3 中它可以工作。我将它重写为 Swift 并创建基本的 GKAgent,GKGoal(toWander:)
这是我的代码:
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!
}
当我添加到场景中
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)
没有行为的位置是静态的,但是当我添加它时,位置变得疯狂,并且在每次更新迭代中,值就像
- 位置 == (-10051366.0, 251672512.0)
- 位置 == (1368370.0, 259904576.0)
- 位置 == (-131583.0, 264841120.0)
它只是一个 Xcode 8 beta 错误,还是我做错了什么。我花了很多时间试图解决它。谢谢:)