关于多点触摸

为了实现多点触控,我写了个onTouchesBegan方法,方法大致如下,即单点和多点实现不同的功效

if (touches.size() == 1){
prevTouch = touches.at(0)->getLocation();
//实现角色普通移动的代码
}else{
//释放技能的代码
}

同时,我又写了个onTouchesMoved方法,方法中:

if (touches.size() == 1){
auto touch = touches.at(0)->getLocation();
if(fabs(touch.x - prevTouch.x)>=100){
//实现角色特殊移动的代码
}}

在模拟器调试时木有问题,因为option键能够保证多点触控完全“同时”,可是真机调试时,我想释放技能,但除非我两个点按得非常“同时”,否则就会变成onTouchesMoved方法,也就是说我先按住一个点,再用另一只手按第二个点都会执行onTouchesMoved方法,请问是哪里有问题,如何实现,谢谢!

有人知道么?我简化下问题:

为什么按住一个点不放,再按第二个点,会被设别为onTouchesMoved,且touches.size() = 1?

楼主,你的问题最后得到解决了吗?我今天也遇到这个问题了!!!!

给你一段lua 代码,关于多点触控的处理的。
–记录当前触摸的数量
local localsize = 0
–多点触控 是否不能点击状态(当容器移动,或缩放,该值为置为true)
TOUCHES_MOVE_FLAG = nil
–防重叠标志
TOUCHES_OVERLAP_FLAG = nil
–触摸开始点,移动中的点,两点距离,x轴的差值,y轴差值
local localBeganPoint,localMovePoint,distance,dx,dy
local function onTouchesBegan(touches,event)
local size = #touches
–记录当前触摸点的数量
localsize = localsize + size
–如果已经有多点触摸 distance 为真,则return
if distance then return end
if size > 1 then
local p1 = touches:getLocation()
local p2 = touches:getLocation()
distance = (p1.x - p2.x)(p1.x - p2.x) + (p1.y - p2.y)(p1.y - p2.y)
–记录偏移
dx = (p1.x + p2.x)/2 - self:getPositionX()
dy = (p1.y + p2.y)/2 - self:getPositionY()
localBeganPoint = nil
TOUCHES_MOVE_FLAG = true
else
–andriod平台多点触摸,触摸点是依次传出
if not localBeganPoint then
localBeganPoint = touches:getLocation()
else
local p2 = touches:getLocation()
distance = (localBeganPoint.x - p2.x)(localBeganPoint.x - p2.x) + (localBeganPoint.y - p2.y)(localBeganPoint.y - p2.y)
–记录偏移
dx = (localBeganPoint.x + p2.x)/2 - self:getPositionX()
dy = (localBeganPoint.y + p2.y)/2 - self:getPositionY()
localBeganPoint = nil
TOUCHES_MOVE_FLAG = true
end
end
end

–触摸移动监听
local function onTouchesMoved(touches,event)
if not self.touchenabled_ then return end
local size = #touches
if size > 1 then
–缩放
if not distance then return end
local touche_1,touche_2
for i = 1,#touches do
if touches*:getId() == 0 then
touche_1 = touches*
elseif touches*:getId() == 1 then
touche_2 = touches*
end
end
if not touche_1 or not touche_2 then return end
local p1 = touche_1:getLocation()
local p2 = touche_2:getLocation()
local tempdistance = (p1.x - p2.x)(p1.x - p2.x) + (p1.y - p2.y)(p1.y - p2.y)
local temp = math.sqrt(math.abs(tempdistance - distance))
–当移动距离大于3像素,则开始执行一次缩放
if temp > 3 then
if tempdistance > distance then
self:excuteScale(self.currentScale_ + self.scaleSteep_)
else
self:excuteScale(self.currentScale_ - self.scaleSteep_)
end
–边界内, 保持偏移
self:excuteMove(cc.p((p1.x+p2.x)/2 - dx,(p1.y+p2.y)/2 - dy))
distance = tempdistance
end
else
–移动
if not localBeganPoint or distance then return end

        local movep = touches:getLocation()
        if not localMovePoint then 
            --设置移动阈值 为 8,即移动 >=8,不应该触发子控件的点击事件
            if math.abs(movep.x - localBeganPoint.x) >= 8 or math.abs(localBeganPoint.y - movep.y) >= 8 then
                TOUCHES_MOVE_FLAG = true
                localMovePoint = movep
            end
        elseif localMovePoint then
            local x,y = self:getPosition()
            self:excuteMove(cc.p(x + (movep.x - localMovePoint.x),y + (movep.y - localMovePoint.y)))
            localMovePoint = movep
        end
    end
end

–触摸结束监听
local function onTouchesEnded(touches,event)
localsize = localsize - #touches
if localsize > 0 then return end
localsize = 0
TOUCHES_MOVE_FLAG = nil
TOUCHES_OVERLAP_FLAG = nil
localBeganPoint = nil
localMovePoint = nil
distance = nil
end


其实多点ios 和android 是不一样的, android的触摸数据是依次回调的,而ios是批量回调(有时候人为的延时触摸也会使多点触摸依次回调), 所以android多点的,began永远只能收到一个点的数据,
上面这段代码 处理了你能想到所有触摸情况(一只手先触摸, 随后再触摸;或者多个个手指一起触摸;再或者先单点触摸移动,然后手指不离开屏幕,在多点缩放; ) , 最后代码里面如果进行过多点缩放,就不能再执行移动。

谢谢楼主!!!!