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

fromJSON

Yuka.js库中的StateMachine类中的fromJSON方法允许从JSON格式的字符串中加载状态机定义。

语法

StateMachine.fromJSON( json, [options] )

参数

  • json:一个字符串,它包含要加载的状态机的JSON格式定义。
  • options(可选):一个对象,表示启用或禁用加载状态机时应采用的选项。

选项

  • useExistingObjects:指示是否允许重用现有状态或行为对象,默认值为false,也就是说,此选项未启用时,将为每个状态和行为创建一个新对象。

示例

const json = `{
  "states": {
    "idle": {
      "entryActions": ["startIdleAnimation"],
      "exitActions": ["stopIdleAnimation"],
      "transitions": [
        {
          "event": "click",
          "targetState": "active",
          "actions": ["playSound"]
        }
      ]
    },
    "active": {
      "entryActions": ["startActiveAnimation"],
      "exitActions": ["stopActiveAnimation"],
      "transitions": [
        {
          "event": "click",
          "targetState": "idle",
          "actions": ["playSound"]
        }
      ]
    }
  }
}`

const stateMachine = StateMachine.fromJSON(json)

上面的示例中,json是一个包含idleactive两个状态的状态机定义的JSON格式字符串。然后,我们使用StateMachine.fromJSON方法将其转换为状态机实例,并将其存储在变量stateMachine中。

返回值

该方法返回一个新创建的StateMachine实例对象。

异常

如果JSON对象中包含错误的格式,fromJSON方法将生成一个SyntaxError类型的异常。如果使用该方法时出现任何其他错误,则将引发未捕获的异常。