解决了触摸兄弟node,点击透明区域下面的node并移动

解决了触摸兄弟node,点击透明区域下面的node并移动(任意节点不是父节点添加监听都可以,因为是通过e.getLocation()判断的,e.getLocation()是世界坐标和节点没有关系,然后可以在touchStart里面判断动态给指定node,然后touchMove里可以移动该指定node,如果移动的_target不是点击的target,则取消点击时是走touchCancel函数,所以要注意逻辑放到touchCancel里判断)

         let positions = this.position_nodes  
        positions.forEach(function(item, index){
            item.index = index
        });
        let relative_positions = this.relative_positions
        let choices = this.choice_nodes
        let okFlag = false
        let _target = null
       async function onTouchBegan(e){
            let target = e.target

            if(okFlag){
                return
            }
           _target = null
            let pos = e.getLocation()
            choices.forEach(function(item){
                item.zIndex = 0
                if(getPointApha(item, pos.x, pos.y)){
                    _target = item
                }
            })

            if(!_target ||  _target.finished){
                return
            }

            _target.zIndex = 1
            that.stopInfo()
            that.playSound(res.ebutton)
            // 统计步数
            updata.finish_steps++        
        }

        async function onTouchMove(e){
            let target = e.target
            if(!_target){
                return
            }
            let delta = e.touch.getDelta();

            _target.x += delta.x * fix
            _target.y += delta.y

        }

        async function onTouchEnd(e) {
            let target = e.target
            if(!_target){
                return
            }
            
            let pos = e.getLocation()
            let _colision = null
            positions.some(function(item){
                if(getPointApha(item, pos.x, pos.y)){
                    _colision = item
                    return true
                }
            })

            if(!_colision || _colision.finished){
                _target.position = _target.getComponent("Node_self").oldPosition
                return
            }

            if(_colision.getComponent("Node_self").flagType !== _target.getComponent("Node_self").flagType){
                that.playSound(res.ewrong)
                _target.position = _target.getComponent("Node_self").oldPosition
                return
            }

            that.playSound(res.eok)
            _target.fadeOut(0.65)
            relative_positions[_colision.index].fadeIn(0.65)
            _target.finished = 1
            _colision.finished = 1
            _colision = null
            _target = null

            if(choices.every(function(item){
                return item.finished
            })){
                okFlag = true
                that.right()
                await sleep(3)
                that.nextPage()
            }

        }
            
       async function onTouchCancel(e) {
            // let target = e.target
            if(!_target){
                return
            }            
            let pos = e.getLocation()
            let _colision = null
            positions.some(function(item){
                if(getPointApha(item, pos.x, pos.y)){
                    _colision = item
                    return true
                }
            })

            if(!_colision || _colision.finished){
                _target.position = _target.getComponent("Node_self").oldPosition
                return
            }
            
            if(_colision.getComponent("Node_self").flagType !== _target.getComponent("Node_self").flagType){
                that.playSound(res.ewrong)
                _target.position = _target.getComponent("Node_self").oldPosition
                return
            }

            that.playSound(res.eok)
            _target.fadeOut(0.65)
            relative_positions[_colision.index].fadeIn(0.65)
            _target.finished = 1
            _colision.finished = 1
            _colision = null
            _target = null

            if(choices.every(function(item){
                return item.finished
            })){
                okFlag = true
                that.right()
                await sleep(3)
                that.nextPage()
            }
        }

        let touchBegan = this.onTouchBegan = onTouchBegan
        let touchMove = this.onTouchMove = onTouchMove
        let touchEnd = this.onTouchEnd = onTouchEnd
        let touchCancel = this.onTouchCancel = onTouchCancel

        for(let i=0, length = choices.length; i<length; i++){
            choices[i].on(cc.Node.EventType.TOUCH_START, touchBegan, this)
            choices[i].on(cc.Node.EventType.TOUCH_MOVE, touchMove, this)
            choices[i].on(cc.Node.EventType.TOUCH_END, touchEnd, this)
            choices[i].on(cc.Node.EventType.TOUCH_CANCEL, touchCancel, this)
        }
···

推荐阅读更多精彩内容