GKCompositeBehavior
一组行为,每一个都是一组目标来影响一个代理运动。
概括
通过将GKGoal对象组成子组(GKBehavior对象)并将这些行为组合成复合行为,你可以控制GKAgent对象某些移动一致行动。 要为代理分配一个行为,请使用其behavior属性。
比方说,你可能为一组代理创建一个行为,当松散地进行跟随路径移动时像一个群体(具有内聚,对齐和分离目标)一般保持一致。使用单个GKBehavior行为对象,每当您想要改变群集目标相对于路径跟踪目标的重要性时,您需要单独更改每个目标的重量。 通过复合行为,您可以将一组目标的相对影响调整在一起,如以下代码所示。
GKBehavior *flock = [GKBehavior behaviorWithGoals:@[
[GKGoal goalToAlignWithAgents:agents maxDistance:10 maxAngle:M_PI_4],
[GKGoal goalToCohereWithAgents:agents maxDistance:10 maxAngle:M_PI_4],
[GKGoal goalToSeparateFromAgents:agents maxDistance:10 maxAngle:M_PI_4]
]];
GKBehavior *meanderOnPath = [GKBehavior behaviorWithgoals:@[
[GKGoal goalToFollowPath:path maxPredictionTime:1.0 forward:YES],
[GKGoal goalToWander:10]
]];
GKCompositeBehavior *composite = [GKCompositeBehavior behaviorWithBehaviors:@[
flock, meanderOnPath
]];
构建此行为后,您可以使用setWeight:forBehavior: 方法来增加或减少flock和meanderOnPath行为相对于彼此的影响。
Topics
构建复合行为(Creating a Composite Behavior)
+ behaviorWithBehaviors:
从指定的单个行为创建复合行为。
+ behaviorWithBehaviors:andWeights:
创建具有指定行为和权重的行为。
在复合行为中管理个体行为(Managing the Individual Behaviors in a Composite Behavior)
- setWeight:forBehavior:
设置指定的单个行为对代理的影响的权重,如果该行为尚未存在,则将该行为添加到复合行为中。
- weightForBehavior:
返回指定的个人行为对代理的影响的权重。
- removeBehavior:
从复合行为中删除指定的单个行为。
- removeAllBehaviors
从复合行为中删除所有单个行为。
behaviorCount
复合行为中个人行为的数量。
使用下标语法处理行为(Working with Behaviors Using Subscript Syntax)
- objectForKeyedSubscript:
返回与由下标语法指定的行为相关联的权重。
- setObject:forKeyedSubscript:
设置由下标语法指定的行为的权重。
- objectAtIndexedSubscript:
返回复合行为行为列表中指定索引处的单个行为。