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函数

描述

sendMessage函数是Yuka js库中的一个触发器(Trigger),用于向其他对象发送消息。

语法

trigger.sendMessage( receiver, message, data )

参数

  • receiver:目标接收者对象,必须为一个有效的Yuka Entity实例。
  • message:字符串类型的消息名称。
  • data:包含任意数据的JavaScript对象,可选参数。

返回值

该函数没有返回值。

示例

调用sendMessage函数

const receiver = new Yuka.Entity();
const message = 'PLAYER_DEATH';
const data = {
    killedBy: 'Monster',
    damage: 60
};

trigger.sendMessage( receiver, message, data );

目标接收者声明消息接收函数

receiver.onMessage = function( message, data ) {
    switch( message ) {
        case 'PLAYER_DEATH':
            console.log( 'Player has been killed by ' + data.killedBy + ' with ' + data.damage + ' damage.' );
            break;

        default:
            console.error( 'Unknown message received: ' + message );
            break;
    }
};

注意事项

  • 该函数只能向Yuka Entity实例对象发送消息。
  • 消息名称应该是字符串类型,建议使用常量来避免拼写错误。
  • 可以在接收者实例的onMessage函数中对不同的消息做出不同的响应。