cocos2d-x,lua的Layer.onClick点击事件封装

来源:互联网 发布:c语言大小写转换循环 编辑:程序博客网 时间:2024/05/22 10:50

cocos2d-x3.4,lua版本Layer封装了onTouch事件,但没有封装onClick事件,游戏按钮资源通常是一张图片,使用ccui.Button有点浪费;

在Layer.onTouch事件中判断是否点击图片比较麻烦,began事件后,移动手势,ended事件坐标点就会跟began事件不在一个点上;

查看widget源码可知,按钮点击事件是在onTouchBegan,onTouchMoved、onTouchEnded中调用hitTest判断图片矩形是否与触摸点一致,都重合时才会触发点击事件;

这里对Layer的onTouch修改下,让其在限定范围内的移动小于20像素(容差值)才触发点击事件,在onClick方法中再判断是否点击目标图片即可,这样可以省去很多触摸判断,也可以实现点击功能,省去ccui.Button类性能也相应得到些提升;

打开文件:src/cocos/framework/extends/LayerEx.lua,编辑以下代码:

--[[Copyright (c) 2011-2014 chukong-inc.comPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.]]local Layer = cc.Layerfunction Layer:onTouch(callback, isMultiTouches, swallowTouches)    if type(isMultiTouches) ~= "boolean" then isMultiTouches = false end    if type(swallowTouches) ~= "boolean" then swallowTouches = false end    self.touchhandler__ = true    local began = nil    self:registerScriptTouchHandler(function(state, ...)        local args = {...}        local event = {name = state}        if isMultiTouches then            args = args[1]            local points = {}            for i = 1, #args, 3 do                local x, y, id = args[i], args[i + 1], args[i + 2]                points[id] = {x = x, y = y, id = id}            end            event.points = points        else            event.x = args[1]            event.y = args[2]        end        local bool = callback(event)                --自定义添加点击事件 by jt 2016/04/20        if self.clickCallBack ~= nil then            if state == "began" then                began = event            elseif state == "ended" then                local offset = 20--误差值,                local rect = cc.rect(began.x-offset,began.y-offset,began.x+offset,began.y+offset)                if event.x >= rect.x and event.y >= rect.y and event.x <= rect.width and event.y <= rect.height then                    self.clickCallBack(event)                end             end        end        return bool    end, isMultiTouches, 0, swallowTouches)    self:setTouchEnabled(true)    return selfendfunction Layer:onClick(callback)    self.clickCallBack = callback    if not self.touchhandler__ then        self:onTouch(function(event)            return true        end)    end    return selfendfunction Layer:removeTouch()    self:unregisterScriptTouchHandler()    self:setTouchEnabled(false)    return selfendfunction Layer:onKeypad(callback)    self:registerScriptKeypadHandler(callback)    self:setKeyboardEnabled(true)    return selfendfunction Layer:removeKeypad()    self:unregisterScriptKeypadHandler()    self:setKeyboardEnabled(false)    return selfendfunction Layer:onAccelerate(callback)    self:registerScriptAccelerateHandler(callback)    self:setAccelerometerEnabled(true)    return selfendfunction Layer:removeAccelerate()    self:unregisterScriptAccelerateHandler()    self:setAccelerometerEnabled(false)    return selfend


使用方法:

local LoginView = class("LoginView",cc.load("mvc").ViewBase)local MotionUtil = {}-------------------------- @param #node---- @param #event ---- @return bool(是否目标触屏对象)function MotionUtil:checkPointToNode(node,event)    if not node:isVisible() then return false end    local point =  node:convertToNodeSpace(event)    local size = node:getContentSize()    local rect = cc.rect(0,0,size.width,size.height)    if cc.rectContainsPoint(rect,point) then        return true    end     return falseendfunction LoginView:ctor()self.spr_btn = cc.Sprite:create("btn_test.png"):addTo(self)--图片路径请自行定义修改--事件层    self.touchLayer = display.newLayer()        :onTouch(handler(self, self.onTouch))        :onClick(handler(self, self.onClick))        :addTo(self)endfunction LoginView:onTouch(event)    --onTouch事件处理,比如触摸按下图片变颜色,松开还原    return true --这里要返回true,onClick才会被调用endfunction LoginView:onClick(event)<span style="white-space:pre"></span>if MotionUtil:checkPointToNode(self.spr_btn,event) thenprint("点中了spr_btn")endend



0 0
原创粉丝点击