3

所以我正在创建一个游戏,我想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 错误,还是我做错了什么。我花了很多时间试图解决它。谢谢:)

4

2 回答 2

2

我知道这已经很老了,但是您是否在第一次更新时以非常高的 deltaTime 调用?

我以系统时间调用作为我对代理行为的第一次更新调用,在第一个时间步引起了巨大的跳跃

于 2019-04-08T14:34:46.717 回答
1

试图解决这个代理,行为的东西是非常令人沮丧的......我在 Xcode 9.0 中遇到了这个问题,当我在我的 SKScenetoSeekAgent的方法中添加我的节点和代理时,但是在从极端位置问题消失了。didMove()touchesBegan()

于 2017-10-13T13:39:20.243 回答