Cocos2dx:3.10+cocostudio多屏幕分辨率适配解决方案

前端之家收集整理的这篇文章主要介绍了Cocos2dx:3.10+cocostudio多屏幕分辨率适配解决方案前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1.设计分辨率
配资源使用的分辨率大小,是1334*750。
2.屏幕分辨率
实际上用户屏幕的分辨率大小,不确定。

这里是实现代码

  1. //
  2. // FixUIUtils.h
  3. // MapTest
  4. //
  5. // Created by Alostz on 16/4/16.
  6. //
  7. //
  8.  
  9. #ifndef FixUIUtils_h
  10. #define FixUIUtils_h
  11. #include "cocos2d.h"
  12.  
  13. using namespace cocos2d;
  14. using namespace std;
  15.  
  16. class FixUIUtils
  17. {
  18. public:
  19. FixUIUtils();
  20. ~FixUIUtils();
  21. static void init();
  22. static void setFixScale(Node *node);
  23. static void setScaleMin(Node* node);
  24. static void setScaleMax(Node* node);
  25. static void setRootNodewithFIXED(Node* node);
  26. static void fixScene(Node* node);
  27. static void fixUI(Node* node);
  28. };
  29.  
  30. #endif /* FixUIUtils_h */

  1. //
  2. // FixUIUtils.cpp
  3. // MapTest
  4. //
  5. // Created by Alostz on 16/4/16.
  6. //
  7. //
  8.  
  9. #include <FixUIUtils.h>
  10. static cocos2d::Size designSize = cocos2d::Size(750,1334);
  11. static cocos2d::Size screenSize;
  12. static float minScale;
  13. static float maxScale;
  14.  
  15. static float xScale;
  16.  
  17. static float yScale;
  18.  
  19. FixUIUtils::FixUIUtils(){
  20.  
  21. }
  22.  
  23. FixUIUtils::~FixUIUtils(){
  24.  
  25. }
  26.  
  27. void FixUIUtils::init(){
  28. screenSize = Director::getInstance()->getVisibleSize();
  29. xScale = designSize.width / screenSize.width;
  30. yScale = designSize.height / screenSize.height;
  31. minScale = MIN(screenSize.height/designSize.height,screenSize.width/designSize.width);
  32. maxScale = MAX(screenSize.height/designSize.height,screenSize.width/designSize.width);
  33. log("xScale = %f * xyScale = %f \n screenSize.width = %f * screenSize.height = %f \n",xScale,yScale,screenSize.width,screenSize.height);
  34.  
  35. }
  36.  
  37. //1、先适配layout层到屏幕大小
  38. void FixUIUtils::setFixScale(Node *node){
  39. auto nodeX = node->getScaleX();
  40. auto nodeY = node->getScaleY();
  41. log("setFixScale nodeX = %f * nodeX = %f \n",nodeX,nodeY);
  42. nodeX = nodeX * xScale;
  43. nodeY = nodeY * yScale;
  44. log("setFixScale nodeX = %f * nodeX = %f \n",nodeY);
  45. node->setScaleX(nodeX);
  46. node->setScaleY(nodeY);
  47. }
  48.  
  49. //屏幕宽、高分别和设计分辨率宽、高计算缩放因子,取较(大)者作为宽、高的缩放因子。
  50. //适用于控件的缩放
  51. void FixUIUtils::setScaleMax(Node *node){
  52. auto nodeX = node->getScaleX();
  53. auto nodeY = node->getScaleY();
  54. log("setScaleMax nodeX = %f * nodeX = %f \n",nodeY);
  55.  
  56. nodeX = nodeX * maxScale;
  57. nodeY = nodeY * maxScale;
  58. log("setScaleMax nodeX = %f * nodeX = %f \n",nodeY);
  59.  
  60. node->setScaleX(nodeX);
  61. node->setScaleY(nodeY);
  62. }
  63.  
  64. //屏幕宽、高分别和设计分辨率宽、高计算缩放因子,取较(小)者作为宽、高的缩放因子。
  65. //适用于背景的缩放
  66. void FixUIUtils::setScaleMin(Node *node){
  67. auto nodeX = node->getScaleX();
  68. auto nodeY = node->getScaleY();
  69. log("setScaleMin nodeX = %f * nodeX = %f \n",nodeY);
  70.  
  71. nodeX = nodeX * minScale;
  72. nodeY = nodeY * minScale;
  73. log("setScaleMin nodeX = %f * nodeX = %f \n",nodeY);
  74.  
  75. node->setScaleX(nodeX);
  76. node->setScaleY(nodeY);
  77. }
  78.  
  79.  
  80. void FixUIUtils::setRootNodewithFIXED(Node* node){
  81. auto moveX = (designSize.width - screenSize.width);
  82. auto moveY = (designSize.height - screenSize.height);
  83. node->setPosition(Point(-moveX,-moveY));
  84. }
  85.  
  86. void FixUIUtils::fixScene(Node* node){
  87. fixUI(node);
  88. FixUIUtils::setRootNodewithFIXED(node);
  89. }
  90.  
  91.  
  92. void FixUIUtils::fixUI(Node* node){
  93. Vector<Node*> ChildrenList = node->getChildren();
  94.  
  95. for (Node* child: ChildrenList) {
  96. log("fixUI nodeX = %d \n",child->getTag());
  97. FixUIUtils::setScaleMin(child);
  98. }
  99. }
  100.  


调用
  1. //加载Cocos Studio编辑好的资源
  2. auto rootNode = CSLoader::createNode("test.csb");
  3. FixUIUtils::init();
  4. FixUIUtils::setFixScale(rootNode);
  5. FixUIUtils::fixScene(rootNode);
  6. this->addChild(rootNode);

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