菜鸟教程小白 发表于 2022-12-12 21:40:53

ios - xcode 8 beta 4 中的 GKBehaviour 让 gkagent 的位置变得疯狂


                                            <p><p>所以我正在创建一个游戏,我想为我的 <code>GKAgent</code> 行为添加一些 <code>GKGoal</code>。</p>
<p>所以经过几个小时的战斗,我这次下载了下一个项目来自 Apple <a href="https://developer.apple.com/library/ios/samplecode/AgentsCatalog/Introduction/Intro.html" rel="noreferrer noopener nofollow">Agents Catalog</a> ,并且在 Xcode 7.3 中它可以工作。我将它重写为 Swift 并使用 <code>GKGoal(toWander:)</code> 创建基本的 GKAgent 这是我的代码:</p>
<pre><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(&#34;init(coder:) has not been implemented&#34;)
    }
   
   
    func agentWillUpdate(_ agent: GKAgent) {
      
    }
   
    func agentDidUpdate(_ agent: GKAgent) {
      self.position = CGPoint(x: Double(self.agent.position.x), y: Double(self.agent.position.y))
      print(&#34;aaa == \(self.position)&#34;)
    }
   
    var agent: GKAgent2D!
}
</code></pre>
<p>当我添加到场景中时</p>
<pre><code>   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)
</code></pre>
<p>没有行为的位置是静态的,但是当我添加它时,位置会变得疯狂,并且在每次更新迭代中,值就像</p>
<ul>
<li>位置 == (-10051366.0, 251672512.0)</li>
<li>位置 == (1368370.0, 259904576.0)</li>
<li>位置 == (-131583.0, 264841120.0)</li>
</ul>
<p>这只是 Xcode 8 测试版错误,还是我做错了什么。我花了很多时间试图解决它。
谢谢:)</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我知道这已经很老了,但是您在第一次更新时是否以非常高的 deltaTime 调用?</p>

<p>我以系统时间调用作为我对代理行为的第一次更新调用,在第一个时间步引起了巨大的跳跃</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - xcode 8 beta 4 中的 GKBehaviour 让 gkagent 的位置变得疯狂,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38778277/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38778277/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - xcode 8 beta 4 中的 GKBehaviour 让 gkagent 的位置变得疯狂