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

toJSON

概述

toJSONCompositeGoal 类的方法之一。该方法将 CompositeGoal 对象转换为 JSON 格式的字符串。 CompositeGoal 表示具有多个子目标(即 Goal 对象)的目标。使用 toJSON 方法可以方便地将 CompositeGoal 对象保存在本地或传输到服务器。

语法

CompositeGoal.toJSON()

参数

该方法不接受参数。

返回值

返回一个 JSON 字符串,其中包含 CompositeGoal 对象的所有成员变量及其对应的值。

示例

代码

const goal1 = new Goal("goal1", true);
const goal2 = new Goal("goal2", true);
const goal3 = new Goal("goal3", false);
const compositeGoal = new CompositeGoal("compositeGoal", [goal1, goal2, goal3]);
console.log(compositeGoal.toJSON());

输出

{
  "name": "compositeGoal",
  "completed": false,
  "subGoals": [
    {
      "name": "goal1",
      "completed": true
    },
    {
      "name": "goal2",
      "completed": true
    },
    {
      "name": "goal3",
      "completed": false
    }
  ]
}

异常

该方法不会抛出异常。

备注

CompositeGoaltoJSON 方法只会将直接子节点转换为 JSON 格式的字符串,不会递归转换其子节点的子节点。如果需要递归转换,请使用递归函数。

参考文献

无。