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

changeTo

描述

changeTo方法用于将状态机的当前状态转换为指定状态。该方法可以被任何状态调用,且只会在调用时才会产生状态转换。该方法的执行遵循以下规则:

  1. 尝试调用当前状态的exit方法;
  2. 尝试调用新状态的enter方法;
  3. 将新状态设置为当前状态。

语法

stateMachine.changeTo(newState);

参数

  • newState:新的状态。该参数可以是状态机中已经定义过的状态,也可以是一个新的状态。如果该参数为状态机中未定义过的状态,则会自动添加该状态到状态机中。

示例

// 定义状态机
let stateMachine = new StateMachine({
  init: 'idle',
  transitions: [
    { name: 'start', from: 'idle', to: 'running' },
    { name: 'complete', from: 'running', to: 'finished' },
    { name: 'reset', from: ['running', 'finished'], to: 'idle' }
  ],
  methods: {
    onEnterRunning: function() {
      console.log('已进入running状态');
    },
    onEnterFinished: function() {
      console.log('已进入finished状态');
    }
  }
});

stateMachine.changeTo('running');
// 输出:已进入running状态

stateMachine.changeTo('finished');
// 输出:已进入finished状态

异常

如果在调用changeTo方法时发生以下情况之一,则会抛出异常:

  • newState参数为undefined
  • newState参数表示的状态未定义;
  • 无法从当前状态转换到目标状态。

参考链接