InputController.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import GameManager from "./GameManager";
  2. const { ccclass, property } = cc._decorator;
  3. @ccclass
  4. export default class InputController extends cc.Component {
  5. touchNum: number = 0;
  6. // LIFE-CYCLE CALLBACKS:
  7. // onLoad () {}
  8. start() {
  9. this.openTouch();
  10. }
  11. // update (dt) {}
  12. openTouch() {
  13. this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
  14. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  15. this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  16. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  17. }
  18. onTouchStart(e) {
  19. if (GameManager.Instance.TargetChara != null) {//检测TargetChara是否为空 空就不执行任何动作
  20. this.touchNum = 1;
  21. let posx = this.node.convertToNodeSpaceAR(e.getLocation()).x;//获取点击位置的x值
  22. let posy = GameManager.Instance.TargetChara.y;//获取上方水果的y值
  23. cc.tween(GameManager.Instance.TargetChara)
  24. .to(0.1, { position: cc.v3(posx, posy, 0) })
  25. .start()
  26. }
  27. }
  28. onTouchMove(e) {
  29. if (GameManager.Instance.TargetChara != null) {
  30. this.touchNum = 1;
  31. GameManager.Instance.TargetChara.x = this.node.convertToNodeSpaceAR(e.getLocation()).x; //水果跟随鼠标移动
  32. }
  33. }
  34. onTouchEnd(e) {
  35. if (GameManager.Instance.TargetChara != null && this.touchNum == 1) { //this.touchNUm 是防止下落位置错误
  36. this.touchNum = 0;
  37. GameManager.Instance.CharaNumberRec[GameManager.Instance.TargetChara.getComponent("CharaCollision").CharaNumber]++;
  38. GameManager.Instance.updateCharaNumber();
  39. // 给TargetChara重新设置物理参数
  40. // 碰撞器的半径为Chara高度的一半
  41. GameManager.Instance.TargetChara.getComponent(cc.PhysicsCircleCollider).radius = GameManager.Instance.TargetChara.height / 2;
  42. // 保存
  43. GameManager.Instance.TargetChara.getComponent(cc.PhysicsCircleCollider).apply();
  44. // 刚体类型改为动态
  45. GameManager.Instance.TargetChara.getComponent(cc.RigidBody).type = cc.RigidBodyType.Dynamic;
  46. // 给一个初始的下落线速度
  47. GameManager.Instance.TargetChara.getComponent(cc.RigidBody).linearVelocity = cc.v2(0, -800)
  48. // 属性改完后,TargetChara设置为空,防止连续操作
  49. GameManager.Instance.TargetChara = null;
  50. this.scheduleOnce(() => {
  51. GameManager.Instance.randomOneChara();
  52. }, 0.5)
  53. }
  54. }
  55. }