【OpenHarmony】鸿蒙开发之 behaviortree
本文介绍了BehaviorTree库在鸿蒙开发中的应用,该库是基于JavaScript的行为树实现,支持Node.js和浏览器环境。文章简要说明了安装方法、核心功能模块(如Task、Sequence、Selector等)的基本用法,并提供了创建行为树实例和遍历的代码示例。
·
往期推文全新看点(文中附带全新鸿蒙5.0全栈学习笔录)
✏️ 鸿蒙应用开发与鸿蒙系统开发哪个更有前景?
✏️ 嵌入式开发适不适合做鸿蒙南向开发?看完这篇你就了解了~
✏️ 对于大前端开发来说,转鸿蒙开发究竟是福还是祸?
✏️ 鸿蒙岗位需求突增!移动端、PC端、IoT到底该怎么选?
✏️ 市场巨变,移动开发行业即将迎来“第二春”?
✏️ 记录一场鸿蒙开发岗位面试经历~
✏️ 持续更新中……
介绍
behaviortree ,是行为树 javascript 版实现,可以运行 node.js 和浏览器中,本库基于 behaviortree 原库 v2.1.0 版本进行验证。
下载安装
ohpm install behaviortree
使用说明
import { BehaviorTree } from "behaviortree";
示例
1.创建一个简单的任务
import { Task, SUCCESS } from "behaviortree";
const myTask = new Task({
// (optional) this function is called directly before the run method
// is called. It allows you to setup things before starting to run
start: function (blackboard) {
blackboard.isStarted = true;
},
// (optional) this function is called directly after the run method
// is completed with either this.success() or this.fail(). It allows you to clean up
// things, after you run the task.
end: function (blackboard) {
blackboard.isStarted = false;
},
// This is the meat of your task. The run method does everything you want it to do.
run: function (blackboard) {
return SUCCESS;
},
});
方法:
- start - 在调用运行之前调用。但如果任务在以 this.running()结束后恢复,则不会
- end - 在调用运行后调用。但如果任务以 this.running()结束,则不会
- run - 包含你希望任务做的事情
2.创建序列
import { Sequence } from "behaviortree";
const mySequence = new Sequence({
nodes: [
// here comes in a list of nodes (Tasks, Sequences or Priorities)
// as objects or as registered strings
],
});
3.创建优先级选择器
import { Selector } from "behaviortree";
const mySelector = new Selector({
nodes: [
// here comes in a list of nodes (Tasks, Sequences or Priorities)
// as objects or as registered strings
],
});
4.创建随机选择器
import { Random } from "behaviortree";
const mySelector = new Random({
nodes: [
// here comes in a list of nodes (Tasks, Sequences or Priorities)
// as objects or as registered strings
],
});
5.创建行为树实例
import { BehaviorTree } from "behaviortree";
var bTree = new BehaviorTree({
tree: mySelector,
blackboard: {},
});
6.遍历行为树
bTree.step();
接口说明
| 方法名 | 接口描述 |
|---|---|
| Sequence | 创建序列 |
| Selector | 创建优先级选择器 |
| Random | 创建随机选择器 |
| BehaviorTree | 创建行为树实例 |
| step | 遍历行为树 |

更多推荐
所有评论(0)