import { GameLogic } from "./GameLogic"; const {ccclass, property} = cc._decorator; @ccclass export default class MoveDest extends GameLogic { private _speed: number = 100; public get speed(): number { return this._speed; } public set speed(value: number) { this._speed = value; } private _destination: cc.Vec2 = cc.v2(0, 0); public get destination(): cc.Vec2 { return this._destination; } public set destination(value: cc.Vec2) { this._destination = value; } x_max: number; y_max: number; protected onLoad(): void { let parentSize = this.node.parent.getContentSize(); let selfSize = this.node.getContentSize(); this.x_max = (parentSize.width - selfSize.width)/2; this.y_max = (parentSize.height - selfSize.height)/2; } gameUpdate(dt: number): void { let offset = this.destination.sub(this.node.getPosition()); if(offset.mag() <= this.speed * dt) { this.node.setPosition(this.destination); } else{ let dirction = offset.normalize(); this.node.x += this.speed * dt * dirction.x; this.node.y += this.speed * dt * dirction.y; } cc.log('@@ -- ', this.node.position.toString()); this.node.x = Math.max(-this.x_max , this.node.x); this.node.x = Math.min(this.x_max , this.node.x); this.node.y = Math.max(-this.y_max , this.node.y); this.node.y = Math.min(this.y_max , this.node.y); } isDone(): boolean { return this.node.x == this._destination.x && this.node.y == this._destination.y; } }