码迷,mamicode.com
首页 > 其他好文 > 详细

COCOS2dx 实现地图缩放和拖动/拖动助力

时间:2015-07-10 22:17:20      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:lua   cocos2dx   屏幕滑动 屏幕缩放   

下面是lua版本的具体代码 

缩放算法大致和http://blog.csdn.net/somestill/article/details/10581277相同,做了一些优化 他的算法有bug

cc.FileUtils:getInstance():addSearchPath("src")
cc.FileUtils:getInstance():addSearchPath("res")

require "game.EngineDefault"

local _layer = nil;
local _scene = nil;
local _tileMap = nil;
local _grassTileLayer = nil;
local _treeTileLayer = nil;
local _player = nil;

local distance;
local deltaX;
local deltaY;
local mscale = 1;
local firsttouch = true;

--滑动助力
local lastMove = nil;
local DIS_MIN = 5;



local function autoMoveMap()
    print("autoMoveMap",lastMove.x,lastMove.y);
    if(math.abs(lastMove.x) <= 2 and math.abs(lastMove.y) <= 2)then
        local px,py = _tileMap:getPosition();
        _tileMap:setPosition(cc.p(px + lastMove.x,py + lastMove.y));
        _layer:unscheduleUpdate();
        lastMove = nil;
        return;
    end

    local px,py = _tileMap:getPosition();
    local moveX = lastMove.x / 1.2;
    local moveY = lastMove.y / 1.2;
    _tileMap:setPosition(cc.p(px + moveX,py + moveY));

    lastMove.x = moveX;
    lastMove.y = moveY;
end

local function onTouchBegin(touch,event)
    firsttouch = true; 
    _layer:unscheduleUpdate();
    return true;
end

local function onTouchMove(touch,event)
    if(#touch == 1)then --single touch
        --重置标志位 防止开始用户使用2个手指缩放 
        --松开一个手指拖动 再用2个手指缩放 不会触发 onTouchBegin 的问题
        firsttouch = true;
        local d = touch[1]:getDelta();
        local scale = _layer:getScale();
        --这里要按照缩放比例来决定滑动的距离 不然在scale较小的情况下会出来 "滑不动"
        d = cc.p(d.x /scale ,d.y/ scale);
        local px,py = _tileMap:getPosition();
        _tileMap:setPosition(cc.p(px + d.x,py + d.y));
        lastMove = d;
    else --multi touch
        lastMove = nil
        
        local p1 = touch[1]:getLocation();
        local p2 = touch[2]:getLocation();
        local pMid = cc.pMidpoint(p1,p2);

        if(firsttouch)then
            firsttouch = false;
            distance = cc.pGetDistance(p1,p2);
            deltaX = pMid.x - _layer:getPositionX();
            deltaY = pMid.y - _layer:getPositionY();
            return ;
        end

        local mdistance = cc.pGetDistance(p1,p2);
        mscale = mdistance/distance * mscale;
        distance = mdistance;
        _layer:setScale(mscale);

        --这个因子是为了解决这个算法 中点飘得问题 当缩放比例较小时 缩放中心点 会比较怪
        local factor = math.min(1,_layer:getScale());
        local x = (pMid.x - deltaX) * factor;
        local y = (pMid.y - deltaY) * factor;
        _layer:setPosition(cc.p(x,y));
        deltaX = pMid.x - _layer:getPositionX();
        deltaY = pMid.y - _layer:getPositionY();

    end


    -- 
end

local function onTouchEnd(touch,event)
    if(#touch == 1)then --single touch

        if(lastMove)then
            --lastMove = cc.pMul(lastMove,5);
            if(math.abs(lastMove.x) <= DIS_MIN and math.abs(lastMove.y) <= DIS_MIN)then
                return;
            end
            _layer:scheduleUpdate(autoMoveMap);
        else
            return;
        end
        
    else --multi touch
    end    
end


local function loadTileMap()
    _tileMap = ccexp.TMXTiledMap:create("map.tmx");
    _treeTileLayer = _tileMap:getLayer("trees");
    _grassTileLayer = _tileMap:getLayer("grass");
    _tileMap:setScale(2);
    return _tileMap;
end

local function createLayer()
    _layer = cc.Layer:create();
    _layer:addChild(loadTileMap());
    _layer:setTouchEnabled(true);

    local listener = cc.EventListenerTouchAllAtOnce:create();
    listener:registerScriptHandler(onTouchBegin,cc.Handler.EVENT_TOUCHES_BEGAN);
    listener:registerScriptHandler(onTouchMove,cc.Handler.EVENT_TOUCHES_MOVED);
    listener:registerScriptHandler(onTouchEnd,cc.Handler.EVENT_TOUCHES_ENDED);

    _layer:getEventDispatcher():addEventListenerWithSceneGraphPriority(listener,_layer);
    return _layer;
end

local function createScene()
    _scene = cc.Scene:create();
    _scene:addChild(createLayer());
    return _scene;
end


local function main()
    cc.Director:getInstance():runWithScene(createScene());
end

xpcall(main, __G__TRACKBACK__)

版权声明:本文为博主原创文章,未经博主允许不得转载。

COCOS2dx 实现地图缩放和拖动/拖动助力

标签:lua   cocos2dx   屏幕滑动 屏幕缩放   

原文地址:http://blog.csdn.net/saint_/article/details/46835143

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!