var winSize = cc.director.getWinSize(); if(touches.length > 1) // 多点进行缩放 { 得到当前两触摸点 point1 = touches[0].getLocation(); var point2 1].getLocation(); 计算两点之间得距离 x1 = point1.x - point2.x; y1 point1.y point2.y; currDistance = Math.pow((x1*+y1y1),0.5);//cc.pDistance(point1,point2); //Math.sqrt(x1*x1+y1*y1);//point1.distance(point2); // 计算两触摸点上一时刻之间得距离 x11 getPrevIoUsLocation().x -getPrevIoUsLocation().x; y11 getPrevIoUsLocation().y - getPrevIoUsLocation().y; prevDistance x11y11y11),94)">//cc.pDistance(touches[0].getPrevIoUsLocation(),touches[1].getPrevIoUsLocation());//Math.sqrt(x11*x11+y11*y11);//touches[0].getPrevIoUsLocation().distance(touches[1].getPrevIoUsLocation()); 两触摸点与原点的差向量,pointVec1和pointVec2是相对于bgSprite的位置 pointVec1 = cc.p(point1.xe.getCurrentTarget().bgOrigin.x,point1.ygetCurrentTarget().bgOrigin.y);//cc.pSub(point1,e.getCurrentTarget().bgOrigin); pointVec2 point2.xpoint2.y//cc.pSub(point2,94); font-family:'宋体'">两触摸点的相对中点 relMidx = (pointVec1.x + pointVec2.x) / 2 ; relMidy pointVec1.y pointVec2.y) 2 ; 计算的锚点 anchorX / getCurrentTarget().bgSprite.getBoundingBox().width; anchorY getBoundingBox().height; 相对屏幕的中点 absMidx point2.x point1.x) absMidy point2.y point1.y) 缩放时,为了避免出现空白的区域,需要做以下的边界处理。 当快要进入到屏幕时,修改的位置(既absMidxabsMidy)。 if( getCurrentTarget().bgOrigin.x 0) { -= getCurrentTarget().bgOrigin.x; } if( getCurrentTarget().x < -getBoundingBox().width winSize.width ) { += -winSize.width getCurrentTarget().bgOrigin.y 0 ) { getCurrentTarget().bgOrigin.y; } getBoundingBox().height winSize.height ) { winSize.height getCurrentTarget().bgOrigin.y; } 重设锚点和位置 setAnchorPoint(cc.anchorX,162)">anchorY)); setPosition(cc.absMidx,162)">absMidy)); 根据两触摸点前后的距离计算缩放倍率 scale getScale() * ( / prevDistance); cc.log("sacel ==== "scale); 控制缩放倍率在1~4倍之间,最小倍率不能太小,不让背景将不能填充满整个屏幕。 min(4,Math.max(1,162)">scale)); setScale(更新原点位置 pt1 absMidy); pt2 p(* anchorY) ; //e.getCurrentTarget().bgOrigin = cc.p(absMidx,absMidy) - cc.p(e.getCurrentTarget().bgSprite.getBoundingBox().width * anchorX,e.getCurrentTarget().bgSprite.getBoundingBox().height * anchorY) ; getCurrentTarget().bgOrigin pt1.x-pt2.x,162)">pt1.ypt2.y);//cc.p(absMidx,e.getCurrentTarget().bgSprite.getBoundingBox().height * anchorY) ; } else if(== 单点进行移动 { 单点时,touches中只有一个Touch对象,所以通过touches[0]就可以得到触摸对象 touch 0]; 计算滑动过程中的滑动增量 diff touch.getDelta(); 得到当前currentPos getPosition(); 得到滑动后应该所在的位置 pos pAdd(currentPos,162)">diff); 得到此刻的尺寸 bgSpriteCurrSize getBoundingBox(); //边界控制,约束pospos.x min(pos.x,162)">bgSpriteCurrSize.width * getAnchorPoint().x); max(pos.y pos.y,162)">bgSpriteCurrSize.height getAnchorPoint().y); getAnchorPoint().y); 位置 setPosition(pos); //if( pos.x >= bgSpriteCurrSize.width * e.getCurrentTarget().bgSprite.getAnchorPoint().x // || pos.x <= -bgSpriteCurrSize.width + winSize.width + bgSpriteCurrSize.width * e.getCurrentTarget().bgSprite.getAnchorPoint().x) //{ // diff.x = 0; //} //if( pos.y >= bgSpriteCurrSize.height * e.getCurrentTarget().bgSprite.getAnchorPoint().y // || pos.y <= -bgSpriteCurrSize.height + winSize.height + bgSpriteCurrSize.height * e.getCurrentTarget().bgSprite.getAnchorPoint().y) // diff.y = 0; //e.getCurrentTarget().bgOrigin += diff; off pSub(pos,162)">currentPos); //e.getCurrentTarget().bgOrigin += off; pAdd(cc.getCurrentTarget().bgOrigin.y),162)">off); }原文链接:https://www.f2er.com/cocos2dx/343860.html