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

sendMessage

MovingEntity的sendMessage方法用于向其他实体发送消息。该方法接受两个参数:消息类型和消息内容。发送的消息可以被其他实体通过receiveMessage方法接收。

语法

movingEntity.sendMessage(messageType, messageContent);

参数

  • messageType:必须是字符串类型,表示消息的类型。
  • messageContent:可以是任意类型,表示发送的消息内容。

示例

以下示例演示如何发送一条消息:

// 创建两个实体
let entity1 = new MovingEntity();
let entity2 = new MovingEntity();

// entity1 发送一条消息给 entity2
entity1.sendMessage("greeting", "hello");

// 在 entity2 中接收消息
entity2.receiveMessage = function(messageType, messageContent){
    console.log("Received message of type '" + messageType + "' with content: " + messageContent);
}

注意事项

  • 若要确保接收到消息,请确保目标实体的receiveMessage方法已正确实现。
  • 发送消息的实体必须处于活跃状态,即isActivated属性为true。
  • 该方法只能发送消息给其他实体,不能给自身发送消息。