--计算某点是否在方向按钮范围 --规定在方向按钮外围构成的矩形为按钮的边界 functionCtrlLayer:isInDirBtn(x, y) if x >= btn_dir_x and x <= (btn_dir_x + btn_dir_w) and y >= btn_dir_y and y <= (btn_dir_y + btn_dir_h) then returntrue else returnfalse end end
--计数某点是否在方向按钮的中心圆圈内 functionCtrlLayer:isInDirBtnCenter(x, y) local tx = math.abs(x-btn_dir_cx) local ty = math.abs(y-btn_dir_cy) local tlen = math.sqrt( math.pow(tx, 2) + math.pow(ty, 2) ) return tlen <= btn_dir_center_r end
--计数某点相对于方向按钮的中心点的方向 --规定方向有up/right/down/left functionCtrlLayer:getDir(x, y) local dir = '' local tx = math.abs(x - btn_dir_cx) local ty = math.abs(y - btn_dir_cy) if x > btn_dir_cx then if y > btn_dir_cy then if tx > ty then dir = 'right' else dir = 'up' end else if tx > ty then dir = 'right' else dir = 'down' end end else if y > btn_dir_cy then if tx > ty then dir = 'left' else dir = 'up' end else if tx > ty then dir = 'left' else dir = 'down' end end end return dir end
--触屏结束 functionCtrlLayer:onTouchEnded(event) local x, y = event.x, event.y ifself:isInDirBtn(x, y) then ifnotself:isInDirBtnCenter(x, y) then local dir = self:getDir(x, y) self.linstener:setDir(dir) end end end