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

failed

目的

failed 函数表示一个目标(goal)执行失败,可以由用户手动调用或在程序执行过程中自动触发。

用法

yuka.failed( message );
  • message:可选参数,表示失败的提示信息。

描述

failed 函数会中止目标的执行并将其状态改变为失败(failed)状态。如果传入了 message 参数,则会将该参数作为失败信息存储在目标对象的 errorMessage 属性中。

当一个目标的状态由 pendingrunning 变为 failed 时,会自动调用其注册的 onFail 回调函数。用户可以使用该回调函数处理目标执行失败的情况。

示例

const yuka = require('yuka');

const goal = new yuka.Goal({
    onExecute: ( agent, deltaTime ) => {
        // 模拟目标的执行过程
        console.log('目标执行中...');
        if ( deltaTime > 10 ) {
            yuka.failed('执行时间过长');
        }
    },
    onFail: ( agent, errorMessage ) => {
        console.log(`目标执行失败: ${ errorMessage }`);
    }
});

goal.execute( agent, 20 );

在上述示例中,我们创建了一个名为 goal 的目标,其在执行过程中会模拟某种操作,并设定最大执行时间为 10 秒。当实际执行时间超过 10 秒时,我们手动调用 failed 函数来中止目标的执行,并将失败信息存储在 errorMessage 属性中。随后,目标的状态变为 failed,触发了注册的 onFail 回调函数,输出了相应的失败信息。