const { ccclass, property } = cc._decorator; @ccclass export default class FMRunFollow extends cc.Component { /**所要追随的人 */ @property({ type: cc.Node, tooltip: '所要追随的人' }) public target: cc.Node = null; /**移动速率 */ @property({ tooltip: "移动速率" }) public speed: number = 10; /**自动开始跟随 */ @property({ tooltip: "自动开始跟随" }) public autoStart: boolean = false; /**标志位,是否移动 */ private isRun: boolean = false; // // onLoad () {} start() { if (this.autoStart) this.run(); } public run() { this.isRun = true; } update(dt: number) { if (!this.isRun || !this.target) return; let X1 = this.target.x - this.node.x; let Y1 = this.target.y - this.node.y; let x1 = X1 > 0 ? 1 : -1; let y1 = Y1 > 0 ? 1 : -1; if (Math.abs(X1) < 10) { x1 = 0; } if (Math.abs(Y1) < 10) { y1 = 0; } this.node.y += y1 * this.speed * dt; this.node.x += x1 * this.speed * dt; } public stop() { this.isRun = false; } }