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.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
T