touch.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { _decorator, Color, Component, EventTouch, Node, Rect, Sprite, Tween, tween, UITransform, Vec2, Vec3 } from 'cc';
  2. import { DirType } from '../data';
  3. import { car } from './car';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('touch')
  6. export class touch extends Component {
  7. private start_node:Vec2 = null
  8. private end_node:Vec2 = null
  9. private mDir:DirType = DirType.NONE
  10. private mCar:car = null
  11. private CurTouchId:number = -1;
  12. public init(car:car){
  13. this.mCar = car
  14. }
  15. start() {
  16. this.node.on(Node.EventType.TOUCH_START,(et:EventTouch)=>{
  17. // let n = this.getClickPos(et.getUILocation().x)
  18. // if(n!=null){
  19. // n.getComponent(Sprite).color = Color.RED
  20. // this.start_node = n
  21. // }
  22. if(this.CurTouchId!=-1&&this.CurTouchId!=et.getID()){
  23. return
  24. }
  25. this.CurTouchId = et.getID()
  26. this.start_node = et.getUILocation()
  27. },this)
  28. this.node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  29. if(this.CurTouchId!=et.getID()){
  30. return
  31. }
  32. if(this.start_node!=null){
  33. // let n = this.getClickPos(et.getUILocation().x)
  34. // if(n!=null&&n!=this.start_node){
  35. // n.getComponent(Sprite).color = Color.RED
  36. // Tween.stopAllByTarget(this.end_node)
  37. // this.end_node = n
  38. // this.reset()
  39. // this.end_node.getComponent(Sprite).color = Color.YELLOW
  40. // // this.end_node = null
  41. // this.moveDis()
  42. // }
  43. this.end_node = et.getUILocation()
  44. this.moveDis()
  45. }
  46. },this)
  47. this.node.on(Node.EventType.TOUCH_END,this._touchEndEvent.bind(this),this)
  48. this.node.on(Node.EventType.TOUCH_CANCEL,this._touchEndEvent.bind(this),this)
  49. }
  50. moveDis(){
  51. let dis = Vec2.distance(this.start_node,this.end_node)
  52. let dir = DirType.NONE
  53. if(this.end_node.x>this.start_node.x){
  54. dir = DirType.RIGHT
  55. }else if(this.end_node.x<this.start_node.x){
  56. dir = DirType.LEFT
  57. }
  58. let isChangeDir = false
  59. if(this.mDir!=dir){
  60. this.mDir = dir
  61. isChangeDir = true
  62. }
  63. this.mCar.setDir(this.mDir)
  64. this.mCar.setDisDir(dis)
  65. // if(isChangeDir){
  66. this.start_node = this.end_node
  67. // }else{
  68. // tween(this.end_node).delay(0.01).call(()=>{
  69. // this.start_node = this.end_node
  70. // }).start()
  71. // }
  72. // console.log("moveDis",dis, this.mDir)
  73. }
  74. reset(){
  75. // this.start_node = null
  76. let list = this.node.children
  77. for (let index = 0; index < list.length; index++) {
  78. const element = list[index];
  79. element.getComponent(Sprite).color = Color.WHITE
  80. }
  81. }
  82. getClickPos(x:number){
  83. let list = this.node.children
  84. for (let index = 0; index < list.length; index++) {
  85. const element = list[index];
  86. let rect = element.getComponent(UITransform).getBoundingBox()
  87. if(rect.contains(new Vec2(x,rect.yMin))){
  88. return element
  89. }
  90. }
  91. return null
  92. }
  93. _touchEndEvent(et:EventTouch){
  94. if(this.CurTouchId==et.getID()){
  95. this.CurTouchId = -1;
  96. this.mDir = DirType.NONE
  97. this.mCar.setDir(this.mDir)
  98. }
  99. }
  100. update(deltaTime: number) {
  101. }
  102. }