moveDest.ts 978 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { GameLogic } from "./update/logic";
  2. const {ccclass, property} = cc._decorator;
  3. @ccclass
  4. export default class MoveDest extends GameLogic {
  5. private _speed: number = 100;
  6. public get speed(): number {
  7. return this._speed;
  8. }
  9. public set speed(value: number) {
  10. this._speed = value;
  11. }
  12. private _destination: cc.Vec2 = cc.v2(0, 0);
  13. public get destination(): cc.Vec2 {
  14. return this._destination;
  15. }
  16. public set destination(value: cc.Vec2) {
  17. this._destination = value;
  18. }
  19. gameUpdate(dt: number): void {
  20. let offset = this.destination.sub(this.node.getPosition());
  21. if(offset.mag() <= this.speed * dt) {
  22. this.node.setPosition(this.destination);
  23. }
  24. else{
  25. let dirction = offset.normalize();
  26. this.node.x += this.speed * dt * dirction.x;
  27. this.node.y += this.speed * dt * dirction.y;
  28. }
  29. }
  30. }