一、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():获取图像的逻辑大小,并不是像素大小
三、textureRect
获取纹理大小,返回一个Rect类型,这个Rect由Vec2和Size类型组成,其实Rect就是表示一个矩形,Vec2表示纹理开始位置,默认Vec2(0,0),Size表示边长。
getTextureRect():获取Rect对象
sprite ->getTextureRect ().getMaxX ():获取长度 sprite ->getTextureRect (). getMaxY():获取宽度
五、scale
缩放操作:
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 ));