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

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
},

 

Logo

这里是一个专注于游戏开发的社区,我们致力于为广大游戏爱好者提供一个良好的学习和交流平台。我们的专区包含了各大流行引擎的技术博文,涵盖了从入门到进阶的各个阶段,无论你是初学者还是资深开发者,都能在这里找到适合自己的内容。除此之外,我们还会不定期举办游戏开发相关的活动,让大家更好地交流互动。加入我们,一起探索游戏开发的奥秘吧!

更多推荐