touch.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. this.start_node = et.getUILocation()
  23. },this)
  24. this.node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  25. if(this.CurTouchId!=-1&&this.CurTouchId!=et.getID()){
  26. return
  27. }
  28. this.CurTouchId = et.getID()
  29. if(this.start_node!=null){
  30. // let n = this.getClickPos(et.getUILocation().x)
  31. // if(n!=null&&n!=this.start_node){
  32. // n.getComponent(Sprite).color = Color.RED
  33. // Tween.stopAllByTarget(this.end_node)
  34. // this.end_node = n
  35. // this.reset()
  36. // this.end_node.getComponent(Sprite).color = Color.YELLOW
  37. // // this.end_node = null
  38. // this.moveDis()
  39. // }
  40. this.end_node = et.getUILocation()
  41. this.moveDis()
  42. }
  43. },this)
  44. this.node.on(Node.EventType.TOUCH_END,this._touchEndEvent.bind(this),this)
  45. this.node.on(Node.EventType.TOUCH_CANCEL,this._touchEndEvent.bind(this),this)
  46. }
  47. moveDis(){
  48. let dis = Vec2.distance(this.start_node,this.end_node)
  49. let dir = DirType.NONE
  50. if(this.end_node.x>this.start_node.x){
  51. dir = DirType.RIGHT
  52. }else if(this.end_node.x<this.start_node.x){
  53. dir = DirType.LEFT
  54. }
  55. let isChangeDir = false
  56. if(this.mDir!=dir){
  57. this.mDir = dir
  58. isChangeDir = true
  59. }
  60. this.mCar.setDir(this.mDir)
  61. this.mCar.setDisDir(dis)
  62. // if(isChangeDir){
  63. this.start_node = this.end_node
  64. // }else{
  65. // tween(this.end_node).delay(0.01).call(()=>{
  66. // this.start_node = this.end_node
  67. // }).start()
  68. // }
  69. // console.log("moveDis",dis, this.mDir)
  70. }
  71. reset(){
  72. // this.start_node = null
  73. let list = this.node.children
  74. for (let index = 0; index < list.length; index++) {
  75. const element = list[index];
  76. element.getComponent(Sprite).color = Color.WHITE
  77. }
  78. }
  79. getClickPos(x:number){
  80. let list = this.node.children
  81. for (let index = 0; index < list.length; index++) {
  82. const element = list[index];
  83. let rect = element.getComponent(UITransform).getBoundingBox()
  84. if(rect.contains(new Vec2(x,rect.yMin))){
  85. return element
  86. }
  87. }
  88. return null
  89. }
  90. _touchEndEvent(et:EventTouch){
  91. this.CurTouchId = -1;
  92. this.mDir = DirType.NONE
  93. this.mCar.setDir(this.mDir)
  94. }
  95. update(deltaTime: number) {
  96. }
  97. }