Search code examples
movecocos2d-xlayerzooming

how to move, zoom in or out the layer like coc in cocos2d-x?


I create a level layer which include all the game stuff, such as background, enemies, bullets, player and other decals, I want to make this layer can be moved, zoom in and out, most likely clash of clans, Is there any suggestion or ideal, thanks.


Solution

  • Suppose your Layer name is exampleLayer

    LAYER MOVEMENT

    In your layers ccTouchMoved function implement the following code

     void ccTouchMoved(CCTouch* touch, CCEvent* event)
        {
            CCPoint diff = touch->getDelta();
            CCPoint currentPos = exampleLayer->getPosition();
            exampleLayer->setPosition(ccpAdd(currentPos, diff));
        }
    

    ZOOM IN AND ZOOM OUT

    void zoomIn()
    {
        exampleLayer->setPosition(-(CCDirector::sharedDirector()->getWinSize().width/2),-(CCDirector::sharedDirector()->getWinSize().height/2)); // for positioning the laer in middle
        exampleLayer->setScale(1); // you can set your scale factor 1 is for example
    }
    
    void zoomOut()
    {
        exampleLayer->setPosition(0,0);
        exampleLayer->setScale(1); // set your desired scale factor
    }
    

    You can call these methods on your zoomIn and zoomOut button, or as per your game design. Hope it will help you.