c++ - cocos2dx 反向缩放图层

标签 c++ cocos2d-x

我有一个 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/

相关文章:

cocos2d-x - 如何在cocos2d-x 3.2中禁用Android中的多点触控

c++ - 无法使用 C++ 和 SFML 2.1 将图像文件加载为纹理

c++ - 用户自定义bool转换的优先级

c++ - 从 VC++ 6 升级到 MSVC 2005 后 MFC 断言失败

c++ - Ifstream 因未知原因失败

xcode - Cocos2d-x OS X 应用程序菜单

c++ - 任何可在 facebook 上分享/发送邮件的 cocos2d-X 功能?

javascript - 如何使用cocos creator在Cocos2d-X/Cocos2d-JS中调用javascript函数并从c++传递值

android - 无法从 android ndk 中的堆栈跟踪中获取行号

c++ - 在 C/C++ 和 std::array 中使用 TCP 时避免阻塞程序