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

触发器 (Trigger) 的 toJSON 方法返回一个 JSON 对象,其中包含触发器的各种属性。

语法

trigger.toJSON()

返回值

该方法将返回一个 JSON 字符串,其中包含以下属性:

属性名 类型 描述
type string 触发器的类型
event string 触发器的事件名称
selector string 触发器的选择器
callback function 触发器的回调函数
options object 触发器的选项参数

示例

const trigger = new yuka.Trigger('click', '#button', () => console.log('Button clicked.'), { once: true });

const json = trigger.toJSON();
console.log(json);

// Output:
// {
//   "type": "Trigger",
//   "event": "click",
//   "selector": "#button",
//   "callback": "() => console.log('Button clicked.')",
//   "options": { "once": true }
// }

注意事项

  • 在 toJSON 方法中,回调函数会被转换为字符串,但在从 JSON 字符串转换回 JavaScript 对象时,需要手动将其转换回函数。
  • 选项参数中可能包含函数作为值,这些函数同样需要手动转换回函数形式。