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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值