我有一个 CCLayer 来保存我所有的游戏对象,我已经实现了缩放和滚动。为了确保您不能滚动到边界之外,我正在计算一个代表屏幕的矩形并使用它来检查矩形是否在边界内。问题是我的计算有误。该层由 scaleFactor 缩放,如下所示:
world->setScale(scaleFactor);
然后我计算滚动矩形:
float scrollWidth = winSize.width * ( 1 / scaleFactor); // winSize is the size of the screen (1280x
float scrollHeight = winSize.height * ( 1 / scaleFactor);
if(scrollRect.l < 0) scrollRect.l = 0;
if(scrollRect.l + scrollWidth > levelWidth) scrollRect.l -= (scrollRect.l + scrollWidth - levelWidth);
scrollRect.r = scrollRect.l + scrollWidth;
world->setPosition(-scrollRect.l, -scrollRect.b);
(比例因子取值在1.0到0.5之间)
这种方法仅在图层缩小到最大或放大到最小时有效,但当 scaleFactor 不是 MAX/MIN 时它是错误的(有一些剩余空间)。 我究竟做错了什么?我也尝试过更改 anchor (它们当前设置为 0,0)但没有成功。
最佳答案
无论你的比例因子是多少,你都可以做到这一点...... 这里 _tileMap 是你的世界
//两个位置黑白求差代码
CCTouch *fingerOne = (CCTouch*)touchArray->objectAtIndex(0);
CCPoint newTouchLocation = fingerOne->getLocationInView();
newTouchLocation = CCDirector::sharedDirector()->convertToGL(newTouchLocation);
newTouchLocation=_tileMap->convertToNodeSpace(newTouchLocation);
CCPoint oldTouchLocation = fingerOne->getPreviousLocationInView();
oldTouchLocation = CCDirector::sharedDirector()->convertToGL(oldTouchLocation);
oldTouchLocation = _tileMap->convertToNodeSpace(oldTouchLocation);
//get the difference in the finger touches when the player was dragging
CCPoint difference = ccpSub(newTouchLocation, oldTouchLocation);
CCPoint ASD=ccpAdd(_tileMap->getPosition(), ccpMult(difference, _tileMap->getScale()));
CCPoint bottomLeft =ASD;
// Bounding Box....
if (bottomLeft.x >0) {
bottomLeft.x = 0;
}
if (bottomLeft.y>0) {
bottomLeft.y = 0;
}
if (bottomLeft.x < -(mapWidth*_tileMap->getScale() - _screenSize.width)) {
bottomLeft.x = -(mapWidth*_tileMap->getScale()- _screenSize.width);
}
if (bottomLeft.y <-(mapHieght*_tileMap->getScale() - _screenSize.height)) {
bottomLeft.y = - (mapHieght*_tileMap->getScale() - _screenSize.height);
}
_tileMap->setPosition(bottomLeft);
希望对你有帮助
关于c++ - cocos2dx 反向缩放图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20473537/