cocos2d-js 实现双指缩放地图效果 和 单点移动效果


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());
两触摸点与原点的差向量,pointVec1pointVec2是相对于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);
    控制缩放倍率在14倍之间,最小倍率不能太小,不让背景将不能填充满整个屏幕。
    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);

}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...