// Learn TypeScript: // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html const {ccclass, property} = cc._decorator; @ccclass export default class Ball extends cc.Component { size: cc.Size; _posX: number; _posY: number; _touch: boolean; _id: number; GameManager: any; tube_Array: number[][];//管子数组 // LIFE-CYCLE CALLBACKS: onLoad () { // this.init(); this._touch = false; this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this); this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this); this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this); this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this); } init(type,id){ this.set_Touch(type) if(this.node.parent.name != "GameArea"){ return; } this.GameManager = this.node.parent.parent.getComponent("GameManager"); this.tube_Array = this.GameManager.tube_Array; this._posX = this.node.x; this._posY = this.node.y; this.size = cc.winSize; this._id = id; } set_Touch(type){ this._touch = type; } start () { } //开始点击,提高层级 touchStart (event) { if(this._touch){ return true; } else{ return false; } } //移动小球,跟随手指移动 touchMove (event) { this.node.zIndex = 2; if(this._touch){ var touchTemp = cc.v2(event.touch._point.x-this.size.width/2,event.touch._point.y-this.size.height/2); this.node.x = touchTemp.x; this.node.y = touchTemp.y; } } //松手,还原层级,判断是否落到玻璃管内 touchEnd (event) { //先判断是否是回收 this.node.zIndex = 0; if(this._touch){ var jg = this.getPos(cc.v2(this.node.x,this.node.y)); if(jg == 0){ this.node.x = this._posX; this.node.y = this._posY; } else if(jg == 1 && this._posX == -210){ this.node.x = this._posX; this.node.y = this._posY; } else if(jg == 2 && this._posX == 0){ this.node.x = this._posX; this.node.y = this._posY; } else if(jg == 3 && this._posX == 210){ this.node.x = this._posX; this.node.y = this._posY; } else{ this.runEnter(jg); } this._posX = this.node.x; this._posY = this.node.y; } } //执行进管子动画 runEnter(jg){ if(jg == 1 && this.tube_Array[0].length < 3){ var height = -100+this.tube_Array[0].length*100; cc.fx.Notifications.emit("moveTube",{id:this._id,tube:0, start_Pos:cc.v2(this._posX,this._posY), end_Pos:cc.v2(-210,height)}); cc.tween(this.node) .to(0.2,{x:-210}) .to(0.25,{y:height}) .call(()=>{ this._posX = this.node.x; this._posY = this.node.y; }) .start(); } else if(jg == 2 && this.tube_Array[1].length < 2){ var height = -100+this.tube_Array[1].length*100; cc.fx.Notifications.emit("moveTube",{id:this._id,tube:1, start_Pos:cc.v2(this._posX,this._posY), end_Pos:cc.v2(0,height)}); cc.tween(this.node) .to(0.2,{x:0}) .to(0.25,{y:height}) .call(()=>{ this._posX = this.node.x; this._posY = this.node.y; }) .start(); } else if(jg == 3 && this.tube_Array[2].length < 1){ var height = -100+this.tube_Array[2].length*100; cc.fx.Notifications.emit("moveTube",{id:this._id,tube:2, start_Pos:cc.v2(this._posX,this._posY), end_Pos:cc.v2(210,height)}); cc.tween(this.node) .to(0.2,{x:210}) .to(0.25,{y:height}) .call(()=>{ this._posX = this.node.x; this._posY = this.node.y; }) .start(); } } getPos(point){ var jg = 0; var tube1 = this.GameManager.tube1;var tube2 = this.GameManager.tube2;var tube3 = this.GameManager.tube3 var rect1 = cc.rect(tube1.x, tube1.y, tube1.width, tube1.height); var rect2 = cc.rect(tube2.x, tube2.y, tube2.width, tube2.height); var rect3 = cc.rect(tube3.x, tube3.y, tube3.width, tube3.height); var nodeRect = cc.rect(point.x, point.y, this.node.width, this.node.height); if (nodeRect.intersects(rect1)) jg = 1; else if (nodeRect.intersects(rect2)) jg = 2; else if (nodeRect.intersects(rect3)) jg = 3; return jg; } // update (dt) { // } }