WanderBehavior的toJSON方法返回一个包含行走行为相关信息的JSON格式对象。
WanderBehavior.toJSON()
该方法返回一个符合以下规范的JSON格式对象:
{
"type": "wander",
"radius": 5,
"distance": 20,
"interval": 1000,
"angleChange": 1
}
其中各字段含义如下:
字段名 | 类型 | 含义 |
---|---|---|
type | string | 行为类型,固定值为"wander" |
radius | number | 行走半径,即在原地转圈时的半径,单位为像素 |
distance | number | 随机行走的距离,单位为像素 |
interval | number | 两次随机行走的间隔时间,单位为毫秒,即行走速度 |
angleChange | number | 每次随机行走的转向角度,单位为度,不能超过180度 |
const wander = new WanderBehavior();
const json = wander.toJSON();
console.log(json);
// 输出: { "type": "wander", "radius": 5, "distance": 20, "interval": 1000, "angleChange": 1 }
该方法仅适用于WanderBehavior实例,调用其他类型的行为的toJSON方法将会抛出异常。