cocos2dx3.0屏幕适配

前端之家收集整理的这篇文章主要介绍了cocos2dx3.0屏幕适配前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近看了几篇关于这个文章,自己试验了一下,这里主要使用FIXED_HEIGHT这种模式,用FIXED_WIDTH也一样。

主要思想是:

ResolutionPolicy::FIXED_HEIGHT
保持传入的设计分辨率高度不变,根据屏幕分辨率修正设计分辨率的宽度。
ResolutionPolicy::FIXED_WIDTH
保持传入的设计分辨率宽度不变,根据屏幕分辨率修正设计分辨率的高度。

只要在AppDelegate.cpp的AppDelegate::applicationDidFinishLaunching()方法中加几句代码即可:

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
		//在Win32下面做测试,假设手机分辨率(屏幕尺寸)是800x480
        glview = GLViewImpl::createWithRect("My Game",Rect(0,800,480),1.0f);
#endif
    }
	director->setOpenGLView(glview);
    // turn on display FPS
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 30);

    auto frameSize = glview->getFrameSize();//获取屏幕尺寸,

	//我们的设计分辨率是800x480
	auto winSize = Size(800,480);
	//设置设计分辨率的和适配模式,进去看setDesignResolutionSize和updateDesignResolutionSize这两个个方法的源码可以知道,
	//下面这句代码走完之后,设计分辨率的高度被设置成480,但是宽度并不是800,而是做了缩放
	director->getOpenGLView()->setDesignResolutionSize(winSize.width,winSize.height,ResolutionPolicy::FIXED_HEIGHT);

	//下面这个是关键
	director->setContentScaleFactor(frameSize.width/director->getOpenGLView()->getDesignResolutionSize().width);

    // create a scene. it's an autorelease object
    auto scene = LoginScene::createScene();
    // run
    director->runWithScene(scene);

    return true;
}

用到了下面这些接口:

复制代码
  1. Director::getInstance()->getOpenGLView()->setDesignResolutionSize() //设计分辨率大小及模式
  2. Director::getInstance()->setContentScaleFactor() //内容缩放因子
  3. FileUtils::getInstance()->setSearchPaths() //资源搜索路径
  4. Director::getInstance()->getOpenGLView()->getFrameSize() //屏幕分辨率
  5. Director::getInstance()->getWinSize() //设计分辨率
  6. Director::getInstance()->getVisibleSize() //设计分辨率可视区域大小
  7. Director::getInstance()->getVisibleOrigin() //设计分辨率可视区域起点
原文链接:https://www.f2er.com/cocos2dx/340035.html

猜你在找的Cocos2d-x相关文章