Restrict Tile Map to its boundaries

Posted by Farooq Arshed on Game Development See other posts from Game Development or by Farooq Arshed
Published on 2013-10-21T12:48:48Z Indexed on 2013/10/21 16:11 UTC
Read the original article Hit count: 334

Filed under:
|

I have loaded a tmx file in cocos2dx and now I am trying to implement panning. I have successfully implemented the panning first part where the map moves. Now I want to restrict the map so it does not display the map beyond its boundary where it shows black screen. I am confused as to how to implement it.

Below is my code any help would be appreciated.

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }

    const char* tmx= "isometric_grass_and_water.tmx";
    _tileMap = new CCTMXTiledMap();
    _tileMap->initWithTMXFile(tmx);
    this->addChild(_tileMap);
    this->setTouchEnabled(true);

    return true;
}

void HelloWorld::ccTouchesBegan(CCSet *touches, CCEvent *event){
    CCSetIterator it;
    for (it=touches->begin(); it!=touches->end(); ++it){
        CCTouch* touch = (CCTouch*)it.operator*();
        CCLog("touches id: %d", touch->getID());
        oldLoc = touch->getLocationInView();
        oldLoc = CCDirector::sharedDirector()->convertToGL(oldLoc);
    }
}

void HelloWorld::ccTouchesMoved(CCSet *touches, CCEvent *event) {

    if (touches->count() == 1) {
        CCTouch* touch = (CCTouch*)( touches->anyObject() );
        this->moveScreen(touch);
    } else if (touches->count() == 2) {
        this->scaleScreen(touches);
    }
}

void HelloWorld::moveScreen(CCTouch* touch) {
    CCPoint currentLoc = touch->getLocationInView();
    currentLoc = CCDirector::sharedDirector()->convertToGL(currentLoc);

    CCPoint moveTo = ccpSub(oldLoc, currentLoc);
    moveTo = ccpMult(moveTo, -1);
    oldLoc = currentLoc;

    this->setPosition(ccpAdd(this->getPosition(), ccp(moveTo.x, moveTo.y)));
}

© Game Development or respective owner

Related posts about tilemap

Related posts about cocos2d-x