AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

onMessage

简介

在Yuka js库中,State类中的onMessage方法是用于处理实体之间的消息通信的。当一个实体需要与其它实体进行通信时,它可以向State发送一条消息,并且可以通过onMessage方法接收到其它实体的消息。

语法

onMessage( sender, message, data )

参数

  • sender:消息发送者的引用。
  • message:消息的类型。
  • data:消息携带的可选数据对象。

示例

class MyEntity extends Entity {
  constructor() {
    super();

    this.stateMachine.setState( 'idle', new MyState() );
  }
}

class MyState extends State {
  constructor() {
    super();

    this.setMessages( {
      'MY_MESSAGE': this.onMyMessage
    } );
  }

  onMyMessage( sender, data ) {
    console.log( 'Received a message of type MY_MESSAGE from', sender, 'with data', data );
  }
}

let entity1 = new MyEntity();
let entity2 = new MyEntity();

entity1.sendMessage( entity2, 'MY_MESSAGE', { foo: 'bar' } );

以上示例中,我们创建了两个实体entity1entity2,并向entity2发送了一个类型为MY_MESSAGE的消息,并携带了一个名为foo的数据对象,接着在MyState类中的setMessages方法中,我们通过onMyMessage方法来处理MY_MESSAGE类型的消息,并打印出接收到的数据和消息发送者的引用。