123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import { _decorator, Color, Component, EventTouch, Node, Rect, Sprite, Tween, tween, UITransform, Vec2, Vec3 } from 'cc';
- import { DirType } from '../data';
- import { car } from './car';
- const { ccclass, property } = _decorator;
- @ccclass('touch')
- export class touch extends Component {
- private start_node:Vec2 = null
- private end_node:Vec2 = null
- private mDir:DirType = DirType.NONE
- private mCar:car = null
- private CurTouchId:number = -1;
- public init(car:car){
- this.mCar = car
- }
- start() {
- this.node.on(Node.EventType.TOUCH_START,(et:EventTouch)=>{
- // let n = this.getClickPos(et.getUILocation().x)
- // if(n!=null){
- // n.getComponent(Sprite).color = Color.RED
- // this.start_node = n
- // }
- if(this.CurTouchId!=-1&&this.CurTouchId!=et.getID()){
- return
- }
- this.CurTouchId = et.getID()
- this.start_node = et.getUILocation()
- },this)
- this.node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
- if(this.CurTouchId!=et.getID()){
- return
- }
- if(this.start_node!=null){
- // let n = this.getClickPos(et.getUILocation().x)
- // if(n!=null&&n!=this.start_node){
- // n.getComponent(Sprite).color = Color.RED
- // Tween.stopAllByTarget(this.end_node)
- // this.end_node = n
- // this.reset()
- // this.end_node.getComponent(Sprite).color = Color.YELLOW
- // // this.end_node = null
- // this.moveDis()
-
- // }
- this.end_node = et.getUILocation()
- this.moveDis()
- }
- },this)
- this.node.on(Node.EventType.TOUCH_END,this._touchEndEvent.bind(this),this)
- this.node.on(Node.EventType.TOUCH_CANCEL,this._touchEndEvent.bind(this),this)
- }
- moveDis(){
- let dis = Vec2.distance(this.start_node,this.end_node)
- let dir = DirType.NONE
- if(this.end_node.x>this.start_node.x){
- dir = DirType.RIGHT
- }else if(this.end_node.x<this.start_node.x){
- dir = DirType.LEFT
- }
- let isChangeDir = false
- if(this.mDir!=dir){
- this.mDir = dir
- isChangeDir = true
- }
- this.mCar.setDir(this.mDir)
- this.mCar.setDisDir(dis)
- // if(isChangeDir){
- this.start_node = this.end_node
- // }else{
- // tween(this.end_node).delay(0.01).call(()=>{
- // this.start_node = this.end_node
- // }).start()
- // }
- // console.log("moveDis",dis, this.mDir)
- }
- reset(){
- // this.start_node = null
- let list = this.node.children
- for (let index = 0; index < list.length; index++) {
- const element = list[index];
- element.getComponent(Sprite).color = Color.WHITE
- }
- }
- getClickPos(x:number){
- let list = this.node.children
- for (let index = 0; index < list.length; index++) {
- const element = list[index];
- let rect = element.getComponent(UITransform).getBoundingBox()
- if(rect.contains(new Vec2(x,rect.yMin))){
- return element
- }
- }
- return null
- }
- _touchEndEvent(et:EventTouch){
- if(this.CurTouchId==et.getID()){
- this.CurTouchId = -1;
- this.mDir = DirType.NONE
- this.mCar.setDir(this.mDir)
- }
-
- }
- update(deltaTime: number) {
-
- }
- }
|