FObjectFly.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import FF from "./FF";
  2. /**
  3. * 道具飞行到主角身上
  4. */
  5. const {ccclass, property} = cc._decorator;
  6. @ccclass
  7. export default class FObjectFly extends cc.Component {
  8. public ff:FF
  9. public move:boolean = false;
  10. update (dt) {
  11. if(this.move && this.ff.mainSprite && this.ff.mainSprite.node){
  12. let node = this.ff.mainSprite.node;
  13. let pos = node.getPosition();
  14. pos.y = pos.y + node.height/2;
  15. if(this.Genzong(pos,dt)){
  16. this.node.destroy();
  17. }
  18. }
  19. }
  20. //跟踪导弹计算移动以及转向角度
  21. private Genzong(targetPosition,dt){
  22. let speed = 700;
  23. var targetPoint = targetPosition;
  24. var point = cc.v2(this.node.x, this.node.y);
  25. var delta = targetPoint.sub(point);
  26. var distance = point.sub(targetPoint).mag();
  27. // var x1 = point.x;
  28. // var y1 = point.y;
  29. // var deltaRotation = 90 -Math.atan2(y2 - y1, x2 - x1) * 180 /Math.PI;
  30. // this.node.rotation=deltaRotation;
  31. if(distance <= 10){
  32. return true;
  33. }
  34. var x2 = point.x + dt*speed * delta.x / distance;
  35. var y2 = point.y + dt*speed * delta.y / distance;
  36. var newPosition = cc.v2(x2, y2);
  37. this.node.setPosition(newPosition);//设置跟踪导弹的位置
  38. }
  39. }