This class mainly provides coordinates at various special positions on the screen. The header file code is attached:
1 class visiblerect 2 {3 Public: 4 static cocos2d: rect getvisiblerect (); // obtain the screen rectangle 5 6 static cocos2d: vec2 left (); // left middle, 7 static cocos2d: vec2 right (); // 8 Static cocos2d: vec2 top (); // 9 static cocos2d: vec2 bottom () in the top (); // base 10 static cocos2d: vec2 Center (); // center 11 static cocos2d: vec2 lefttop (); // Left top 12 static cocos2d :: vec2 righttop (); // right top 13 static cocos2d: vec2 leftbottom (); // left bottom 14 static cocos2d: vec2 rightbottom (); // right bottom 15 private: 16 static void lazyinit (); // obtain the screen rectangle 17 static cocos2d: rect s_visiblerect; // used to save the screen rectangle 18 };
The above is a function for obtaining the special position of a rectangle. The approximate position is shown as follows:
Rect (X, Y, width, height) is based on (0, 0) in the upper left corner of the screen, width is width, and height is height. In the Cocos2d-x, it can be represented step by step as follows:
Vec2 origin; // coordinate of the upper left corner (x, y)
Size size; // size (width, height)
In fact, this is a member variable in the rect class that represents X, Y, width, and height.
Next let's take a look at the visiblerect. cpp file. The code is attached:
1 void VisibleRect::lazyInit() 2 { 3 s_visibleRect = Director::getInstance()->getOpenGLView()->getVisibleRect(); 4 } 5 6 Rect VisibleRect::getVisibleRect() 7 { 8 lazyInit(); 9 return s_visibleRect;10 }11 // (0,width/2)12 Vec2 VisibleRect::left()13 {14 lazyInit();15 return Vec2(s_visibleRect.origin.x, s_visibleRect.origin.y+s_visibleRect.size.height/2);16 }17 // (width,height/2)18 Vec2 VisibleRect::right()19 {20 lazyInit();21 return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width, s_visibleRect.origin.y+s_visibleRect.size.height/2);22 }23 // (width/2,height)24 Vec2 VisibleRect::top()25 {26 lazyInit();27 return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width/2, s_visibleRect.origin.y+s_visibleRect.size.height);28 }29 // (width/2,0)30 Vec2 VisibleRect::bottom()31 {32 lazyInit();33 return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width/2, s_visibleRect.origin.y);34 }35 // (width/2,height/2)36 Vec2 VisibleRect::center()37 {38 lazyInit();39 return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width/2, s_visibleRect.origin.y+s_visibleRect.size.height/2);40 }41 // (0,height)42 Vec2 VisibleRect::leftTop()43 {44 lazyInit();45 return Vec2(s_visibleRect.origin.x, s_visibleRect.origin.y+s_visibleRect.size.height);46 }47 // (width,height)48 Vec2 VisibleRect::rightTop()49 {50 lazyInit();51 return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width, s_visibleRect.origin.y+s_visibleRect.size.height);52 }53 // (0,0)54 Vec2 VisibleRect::leftBottom()55 {56 lazyInit();57 return s_visibleRect.origin;58 }59 // (width,0)60 Vec2 VisibleRect::rightBottom()61 {62 lazyInit();63 return Vec2(s_visibleRect.origin.x+s_visibleRect.size.width, s_visibleRect.origin.y);64 }
This class is easy to implement, that is, to obtain the coordinates of special locations.
Visiblerect class for Cocos2d-x_3.2 demo Learning