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

描述

toJSON是Yuka js库的状态(State)类中的一个方法,其作用是将当前状态对象转换为JSON格式。

语法

state.toJSON();

返回值

返回当前状态对象转换为的JSON格式数据。

示例

import { State } from 'yuka';

class MyState extends State {
  constructor() {
    super();
    this.hp = 100;
    this.mp = 50;
  }
}

const state = new MyState();

const json = state.toJSON();

console.log(json); // {"hp":100,"mp":50}

注意事项

  • toJSON方法不接收任何参数。
  • toJSON方法会将状态对象中的所有可枚举属性(默认情况下不会枚举原型链上的属性)转换为JSON格式数据,并返回该数据。
  • 如果状态对象中有无法转换为JSON格式的属性(例如函数),则该属性在JSON数据中会被忽略。
  • 转换出的JSON数据不包括状态对象的原型链上的属性和方法。

参考链接

  • JSON.stringify():A method that converts a JavaScript value to a JSON string.