[6]Cocos2d-x之关于屏幕大小与节点大小、节点缩放问题

一、WinSize、VisibleOrigin、VisibleSize、winSizePixels 与DesignResolutionSize

看以下代码:

//returns the size of the OpenGL view in points.以点的形式返回OpenGL视图大小
Vec2 _winSize = Director:: getInstance ()->getWinSize ();
//returns visible origin of the OpenGL view in points.以点的形式返回OpenGL视图的可视开始原点
Vec2 visibleOriginSize = Director ::getInstance ()-> getVisibleOrigin ();
//returns visible size of the OpenGL view in points.以点的形式返回OpenGL视图可视大小
//the value is equal to getWinSize if don't invoke
Vec2 visitSize = Director ::getInstance ()-> getVisibleSize ();
//returns the size of the OpenGL view in pixels.以像素形式返回OpenGL视图的大小
Vec2 winSizePixels = Director :: getInstance()-> getWinSizeInPixels ();
CCLOG ("winSize width:%f",_winSize. width );
CCLOG ("winSize height:%f",_winSize. height );
CCLOG ("visibleOriginSize width:%f",visibleOriginSize . x);
CCLOG ("visibleOriginSize height:%f",visibleOriginSize . y);
CCLOG ("visitSize width:%f",visitSize . x);
CCLOG ("visitSize height:%f",visitSize. y);
CCLOG ("winSizePixels width:%f",winSizePixels . x);
CCLOG ("winSizePixels height:%f",winSizePixels . y);
//结果
winSize width:960.000000
winSize height:640.000000
visibleOriginSize width:0.000000
visibleOriginSize height:0.000000
visitSize width:960.000000
visitSize height:640.000000
winSizePixels width:960.000000
winSizePixels height:640.000000

//修改DesignResolutionSize 
glview->setFrameSize(640,640); //修改cocos2dx的模拟器大小
glview -> setDesignResolutionSize (854,468,ResolutionPolicy :: EXACT_FIT);
//结果:模拟器变小了但是winSizePixels、winsize与visibleSize的值与DesignResolutionSize一致,所以
//winSize并不是指屏幕的大小。
winSize width:854.000000
winSize height:468.000000
visibleOriginSize width:0.000000
visibleOriginSize height:0.000000
visitSize width:854.000000
visitSize height:468.000000
winSizePixels width:854.000000
winSizePixels height:468.000000

总结:
1、winSizePixels、winSize、visitSize的值与DesignResolutionSize一致。
2、visibleOriginSize 获取OpenGl视图开始点,为Vec2(0,0)。

二、contentSize
返回值为Size类型
getContentSize():获取图像的逻辑大小,并不是像素大小
setContentSize():修改图像的逻辑大小,使用setContentSize修改图像后,getContentSize获取的就是修改后的大小。

三、textureRect
获取纹理大小,返回一个Rect类型,这个Rect由Vec2和Size类型组成,其实Rect就是表示一个矩形,Vec2表示纹理开始位置,默认Vec2(0,0),Size表示边长。
getTextureRect():获取Rect对象
sprite ->getTextureRect ().getMaxX ():获取长度
sprite ->getTextureRect (). getMaxY():获取宽度

四、boundingBox
官方解释:Returns an AABB (axis-aligned bounding-box) in its parent's coordinate system.
boundingBox指包围盒(AABB),返回结果类型Rect,但是它与textureRect不同,boundingBox的Vec2值是相对于父节点的。
scene1 ->getBoundingBox ().size.width;
scene1 ->getBoundingBox ().size.height;

五、scale
缩放操作:
setScale()操作对contentSize和textureRect的值没有影响,但是对boundingBox有影响。所以想要获取一个节点缩放后的大小,使用boundingBox。
以下代码可以通过winSize与sprite的宽高比修改sprite的大小与屏幕大小一致:
Size wins = Director :: getInstance()-> getWinSize ();
Sprite * sprite = Sprite :: create( fileName );
float winw = wins .width ; //获取宽度
float winh = wins .height ; //获取高度
float spx = sprite ->getTextureRect (). getMaxX();
float spy = sprite ->getTextureRect (). getMaxY();
sprite ->setScaleX ( winw / spx ); //设置精灵宽度缩放比例
sprite ->setScaleY ( winh / spy );
sprite ->setAnchorPoint ( Vec2( 0,0 ));

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...