import gameUI from "../game/UIFace/gameUI"; const {ccclass, property} = cc._decorator; @ccclass export default class path3 extends cc.Component { private anim: cc.Animation = null; private path3data: cc.Vec3[] = []; public static instance: path3 = null; onLoad() { path3.instance = this; //获取当前节点上的动画组件 this.anim = this.node.getComponent(cc.Animation); // this.anim.play("path3_1"); } /**编辑路径 */ public changeLevelData(): void { this.path3data = []; if (this.anim == null) { return; } //获取动画片段 let clips = this.anim.getClips(); //获取当前片段 let clip = clips[gameUI.instance.myDataObj.LevelData.scene_path - 1]; if (clip) { //获取贝塞尔曲线路径 let frames = clip.curveData.props.position; this.genpath3Data(frames); } } private genpath3Data(frames): void { let bezier = []; //贝塞尔曲线定义四个点:起点、终点、两个相互分离中间控制点点 [begin, ctrl1, ctrl2, end] let begin = null, ctrl1 = null, ctrl2 = null, end = null; //遍历关键帧获取贝塞尔曲线 for (let i = 0; i < frames.length; i++) { const frame = frames[i]; //获取控制起点 if (ctrl1 !== null) { bezier.push([begin, ctrl1, ctrl1, cc.v3(frame.value[0], frame.value[1])]); } //获取起点坐标 begin = cc.v3(frame.value[0], frame.value[1]); //获取关键帧的运动路径 const motionPath = frame.motionPath; //遍历关键帧运动路径获取贝塞尔曲线的四个点 for (let j = 0; j < motionPath.length; j++) { const item = motionPath[j]; //获取终点 end = cc.v3(item[0], item[1]); //获取控制终点 ctrl2 = cc.v3(item[2], item[3]); //判断控制起点是否为空 if (ctrl1 === null) { ctrl1 = ctrl2; } //添加贝塞尔曲线中各点坐标 bezier.push([begin, ctrl1, ctrl2, end]); //获取控制起点 ctrl1 = cc.v3(item[4], item[5]); //设置下一个节点的起点为上一个节点的终点 begin = end; } } // //将贝塞尔曲线转换为坐标点 // const first:cc.Vec3 = bezier[0][0];//获取起始点坐标 // let data = [first]; let data = []; for (let k = 0; k < bezier.length; k++) { const item = bezier[k]; data.push(item[0]); if (k == bezier.length - 1) { data.push(item[3]); } } this.path3data = data; } /**获取路径 */ public getpathData() { return this.path3data; } }