Cocos lua给控件创建触摸事件处理

Cocos lua给控件创建触摸事件处理

1.单点触摸

	--测试代码,新增Label
    local txt_label = cc.LabelTTF:create("测试触摸","Courier",50);
    txt_label:addTo(self)
    txt_label:setPosition(yl.WIDTH/2,yl.HEIGHT/2 - 100);
    self.txt_label = txt_label
    
    
	--单点触摸监听
    local txtlisteneronebyone = cc.EventListenerTouchOneByOne:create();


    --单点触摸处理函数
    local function onTouchBegan( touch , event)
        local pos = touch:getLocation()
        pos = self.txt_label:convertToNodeSpace(pos)
        local rec = cc.rect(0, 0, self.txt_label:getContentSize().width, self.txt_label:getContentSize().height)
        if true == cc.rectContainsPoint(rec, pos) then
            --print("===========单点触摸开始=============")
        end
        return true --返回true,代表事件继续传递,后续可执行move,ended等事件.  返回false则终止事件传递
    end
    local function onTouchMove( touch , event)
        local pos = touch:getLocation()
        pos = self.txt_label:convertToNodeSpace(pos)
        local rec = cc.rect(0, 0, self.txt_label:getContentSize().width, self.txt_label:getContentSize().height)
        if true == cc.rectContainsPoint(rec, pos) then
            --print("===========单点移动开始=============")
            self.txt_label:setPosition(touch:getLocation())
        end
        return true
    end
    local function onTouchEnded( touch , event)
        local pos = touch:getLocation()
        pos = self.txt_label:convertToNodeSpace(pos)
        local rec = cc.rect(0, 0, self.txt_label:getContentSize().width, self.txt_label:getContentSize().height)
        if true == cc.rectContainsPoint(rec, pos) then
            print("===========单点触摸结束=============")
            self.txt_label:setPosition(yl.WIDTH/2,yl.HEIGHT/2 - 100)
        end
        return true
    end
    
	--注册单点触摸
    txtlisteneronebyone:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
    txtlisteneronebyone:registerScriptHandler(onTouchMove,cc.Handler.EVENT_TOUCH_MOVED )
    txtlisteneronebyone:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
    
     --把事件监听添加进事件管理器
    local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(txtlisteneronebyone, self.txt_label)

2.多点触摸

	--测试代码,新增两个Label
    local txt_label = cc.LabelTTF:create("测试触摸","Courier",50);
    txt_label:addTo(self)
    txt_label:setPosition(yl.WIDTH/2,yl.HEIGHT/2 - 100);
    self.txt_label = txt_label

    local txt_label2 = cc.LabelTTF:create("测试触摸","Courier",50);
    txt_label2:addTo(self)
    txt_label2:setPosition(yl.WIDTH/2,yl.HEIGHT/2 + 300);
    self.txt_label2 = txt_label2
    
 	--创建多点触摸监听
    local txtlistenerallatonce = cc.EventListenerTouchAllAtOnce:create();

	--多点触摸处理函数
    local function onTouchesBegan( touches , event)
        for k,v in ipairs(touches) do			--多点触摸传递进来是一个容器,用遍历来读取元素
            local pos = self:convertToNodeSpace(v:getLocation())
            local box1 = self.txt_label:getBoundingBox()
            local box2 = self.txt_label2:getBoundingBox()
            if true == cc.rectContainsPoint(box1, pos) or true == cc.rectContainsPoint(box2, pos) then
                print("多点触摸开始")
                return true --返回true,代表事件继续传递,后续可执行move,ended等事件.  返回false则终止事件传递
            end
        end
    end
    local function onTouchesMove( touches , event)
        for k,v in ipairs(touches) do
            local pos = self:convertToNodeSpace(v:getLocation())
            local box1 = self.txt_label:getBoundingBox()
            local box2 = self.txt_label2:getBoundingBox()
            if true == cc.rectContainsPoint(box1, pos) or true == cc.rectContainsPoint(box2, pos) then
                print("多点触摸移动")
                if true == cc.rectContainsPoint(box1, pos) then 
                    self.txt_label:setPosition(v:getLocation())
                elseif true == cc.rectContainsPoint(box2, pos) then
                    self.txt_label2:setPosition(v:getLocation())
                end
                return true --返回true,代表事件继续传递,后续可执行move,ended等事件.  返回false则终止事件传递
            end
        end
    end
    local function onTouchesEnded( touches , event)
        print("多点触摸结束")
        return true 
    end


    --注册多点触摸
    txtlistenerallatonce:registerScriptHandler(onTouchesBegan,cc.Handler.EVENT_TOUCHES_BEGAN)
    txtlistenerallatonce:registerScriptHandler(onTouchesMove,cc.Handler.EVENT_TOUCHES_MOVED)
    txtlistenerallatonce:registerScriptHandler(onTouchesEnded,cc.Handler.EVENT_TOUCHES_ENDED)
    
	--把事件监听添加进事件管理器
    local eventDispatcher =  cc.Director:getInstance():getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(txtlistenerallatonce, self)

你可能感兴趣的:(Lua/Cocos2d)