有时候明明设置了组件在某一个位置,从逻辑上分析也是正确的,但是实际运行时却总是不出现在所设定的位置。对于大的图片或地图偏移尤其严重。(前提是使用了winsize或contentsize获取了窗口的大小,并且根据了窗口大小来设置图片的位置)
这是由于AppDelegate.cpp中屏幕适配处理的问题,AppDelegate.cpp在初始化时自动给原来的窗口进行一定比例的放大或缩小,若要解决上述错位的问题,只需要去掉该cpp中关于屏幕适配的处理即可。
删除以下:
// Set the design resolution glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); auto frameSize = glview->getFrameSize(); // if the frame's height is larger than the height of medium size. if (frameSize.height > mediumResolutionSize.height) { director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); } // if the frame's height is larger than the height of small size. else if (frameSize.height > smallResolutionSize.height) { director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); } // if the frame's height is smaller than the height of medium size. else { director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); }片