cocos creator 触摸事件 + 移动Node

功能流程  一个张片点击后 移动到 另一张图片上 

start () {
    this.touchNode.on(cc.Node.EventType.TOUCH_START,this.TOUCH_START,this);
    this.touchNode.on(cc.Node.EventType.TOUCH_MOVE,this.TOUCH_MOVE,this);
    this.touchNode.on(cc.Node.EventType.TOUCH_END,this.TOUCH_END,this)
    this.touchNode.on(cc.Node.EventType.TOUCH_CANCEL,this.TOUCH_CANCEL,this);
},

TOUCH_START(event)
{
        //this.imageNode 子节点是点击目标
        for (let index = 0; index < this.imageNode.children.length; index++) {
            const element = this.imageNode.children[index];
            if(cc.rect(element.getBoundingBoxToWorld()).contains(event.getLocation()))
            {
                this.selectNode = element
                this.nowSelectNode = this.createMoveNode(element)
                element.active = false
                break;
            }
        }
},
TOUCH_MOVE(event)
{
       var delta = event.getDelta();
        if (this.nowSelectNode)
        {
            this.nowSelectNode.x += delta.x
            this.nowSelectNode.y += delta.y
        }
},

TOUCH_END(event)
{
        // this.audioNode 是移动触碰目标
        let isP =false
        for (let index = 0; index < this.audioNode.children.length; index++) {
            const element = this.audioNode.children[index];
            if(this.nowSelectNode){
                
               if(cc.rect(element.getBoundingBoxToWorld()).contains(event.getLocation()))                            
                {
                    console.log(element)
                    if(element.userData == this.nowSelectNode.userData){
                       /*  console.log("----- ")
                        console.log(element.userData)
                        console.log(this.nowSelectNode.userData)
                        console.log("----- ") */
                        isP = true;
                        element.active = false
                        break;
                        
                    }
                }
            }
        }
        if(this.selectNode){
            this.selectNode.active =!isP
            this.selectNode = null
        }

        if (this.nowSelectNode){
            this.nowSelectNode.parent = null
            this.nowSelectNode = null
        }
},

TOUCH_CANCEL(event)
{
         if (this.nowSelectNode){
            this.nowSelectNode.parent = null
            this.nowSelectNode = null
        }
        if(this.selectNode){
            this.selectNode.active =true
            this.selectNode = null
        }
},

createMoveNode(node){
        let newNode =cc.instantiate(node)
        newNode.x = node.x
        newNode.y = node.y
        newNode.userData = node.userData
        console.log(newNode)
        this.touchNode.addChild(newNode)
        return newNode
},

 

你可能感兴趣的:(cocos,creator,cocos-creator)