[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 ));





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值