how to prevent showing outside of world game in cocos2dx

Posted by HRZ on Game Development See other posts from Game Development or by HRZ
Published on 2014-06-03T18:47:34Z Indexed on 2014/06/03 21:41 UTC
Read the original article Hit count: 180

Filed under:
|
|

Im trying to make a tower defence game and it can zoom in/out and scrolling over my world map.How to scroll over the game and how to restrict it to don't show outside of my map. At below I scroll over the map by using CCCamera but i don't know how i can restrict it.

CCPoint tap = touch->getLocation();
CCPoint prev_tap = touch->getPreviousLocation();
CCPoint sub_point = tap - prev_tap;

float xNewPos, yNewPos;
float xEyePos, yEyePos, zEyePos;
float cameraPosX, cameraPosY, cameraPosZ;

// First we get the current camera position.
GameLayer->getCamera()->getCenterXYZ(&cameraPosX, &cameraPosY, &cameraPosZ);
GameLayer->getCamera()->getEyeXYZ(&xEyePos, &yEyePos, &zEyePos);

// Calculate the new position
xNewPos = cameraPosX - sub_point.x;
yNewPos = cameraPosY - sub_point.y;

GameLayer->getCamera()->setCenterXYZ(xNewPos, yNewPos, cameraPosZ);
GameLayer->getCamera()->setEyeXYZ(xNewPos, yNewPos, zEyePos);

And for zooming i used such code:

GameLayer->setScale(this->getScale() + 0.002); //zooming in

© Game Development or respective owner

Related posts about camera

Related posts about cocos2d-x