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

handleMessage

简介

handleMessage是Yuka js库中StateMachine类的一个方法。StateMachine是一个状态机,可以使用handleMessage方法将消息发送到该状态机并处理。

用法

handleMessage方法的语法如下:

handleMessage( message )

message参数表示要发送到状态机的消息。

返回值

该方法没有返回值。

示例

以下是使用StateMachine类的handleMessage方法的示例代码:

class TestStateMachine extends StateMachine {
  constructor() {
    super()

    this.states = [
      new State( 'state1' ),
      new State( 'state2' ),
      new State( 'state3' )
    ]

    this.transitions = [
      new Transition( 'state1', 'state2', 'message1' ),
      new Transition( 'state2', 'state3', 'message2' ),
      new Transition( 'state3', 'state1', 'message3' )
    ]

    this.initialize( 'state1' )
  }
}

const fsm = new TestStateMachine()

fsm.handleMessage( 'message1' )

console.log( fsm.currentState ) // 'state2'

上面的示例创建了一个名为TestStateMachine的状态机,并且定义了三个状态和三个转换。然后,使用initialize方法将状态机的初始状态设置为'state1'。接着,使用handleMessage方法将消息'message1'发送到状态机,并执行了一次状态转换。最后,将状态机的当前状态输出到控制台上,结果为'state2'。

注意:在示例代码中,需要在外部先导入StateMachine、State和Transition类。