GKAgent不动

时间:2015-09-09 16:05:29

标签: xcode ios9 gameplay-kit

我的项目中有一个GKAgent,我试图按照路径移动。路径已经与其相应的目标和行为一起创建,只要我从未设置其位置,它就会移动。不幸的是我不希望它从屏幕的角落开始,但如果我设置了位置,代理将不会对其跟随路径目标作出反应。有没有人对此问题有任何见解?

    let path1 = GKPath(graphNodes: path, radius: Float(2.0))

    let moveGoal = GKGoal(toFollowPath: path1, maxPredictionTime: 1.0, forward: true)
    let stayGoal = GKGoal(toStayOnPath: path1, maxPredictionTime: 1.0)
    let behavior = GKBehavior(goals: [moveGoal, stayGoal] , andWeights: [5.0, 10.0])

    agent.maxSpeed = 100
    agent.maxAcceleration = 50
    agent.radius = 40.0
    agent.behavior = behavior
    agent.delegate = self
    addComponent(agent)

func agentWillUpdate(agent: GKAgent) {
    updateAgentPositionToNode()
}

func agentDidUpdate(agent: GKAgent) {
    updateSpritePositionToAgent()
}

func updateSpritePositionToAgent() {
    print(agent.position)

    let agentPos = CGPoint(x: CGFloat(agent.position.x), y: CGFloat(agent.position.y))

    renderComponent.spriteNode.position = CGPoint(x: CGFloat(agentPos.x), y: CGFloat(agentPos.y))

}

func updateAgentPositionToNode() {
    let renderComp = self.renderComponent

    agent.position = float2(Float(renderComp.spriteNode.position.x), Float(renderComp.spriteNode.position.y))
}

1 个答案:

答案 0 :(得分:0)

我删除了停止目标并手动添加