下面是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__)原文链接:https://www.f2er.com/cocos2dx/342346.html