COCOS2dx enables MAP scaling, dragging, and dragging

Source: Internet
Author: User

COCOS2dx enables MAP scaling, dragging, and dragging

The code for lua is as follows:

The scaling algorithm is similar to http://blog.csdn.net/somestill/article/details/10581277. some optimizations to this algorithm have bugs.

Cc. fileUtils: getInstance (): addSearchPath (src) cc. fileUtils: getInstance (): addSearchPath (res) require game. enginedefalocal 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; -- Slide assist 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; endlocal function onTouchBegin (touch, event) firsttouch = true; _ layer: unscheduleUpdate (); return true; endlocal function onTouchMove (touch, event) if (# touch = 1) then -- single touch -- reset the flag to prevent the start user from scaling with two fingers -- releasing one finger and dragging with two fingers will not trigger the onTouchBegin issue firsttouch = true; local d = touch [1]: getDelta (); local scale = _ layer: getScale (); -- The sliding distance must be determined based on the scaling ratio. Otherwise, the sliding distance will not change if the scale is small. 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 ); -- this factor is used to solve the point floating problem in this algorithm. When the scale ratio is small, the center point will be more strange. 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 -- endlocal 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 endlocal function loadTileMap () _ tileMap = ccexp. TMXTiledMap: create (map. tmx); _ treeTileLayer = _ tileMap: getLayer (trees); _ grassTileLayer = _ tileMap: getLayer (grass); _ tileMap: setScale (2); return _ tileMap; endlocal 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; endlocal function createScene () _ scene = cc. scene: create (); _ scene: addChild (createLayer (); return _ scene; endlocal function main () cc. director: getInstance (): runWithScene (createScene (); endxpcall (main, _ g1_trackback __)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.