Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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@R_944_6334@;

    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 实现地图缩放和拖动/拖动助力全部内容,希望文章能够帮你解决COCOS2dx 实现地图缩放和拖动/拖动助力所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。