How to prevent showing outside of world game in Cocos2D-x

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/07 15:41 UTC
Read the original article Hit count: 239

Filed under:
|
|

I'm trying to make a tower defense game and it can zoom in/out and scrolling over my world map. How to scroll over the game and how to restrict it not to show outside of my map(black area).

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(GameLayer->getScale() + 0.002); //zooming in

© Game Development or respective owner

Related posts about camera

Related posts about cocos2d-x