关于此篇的代码,其实可以从tests\cpp-tests的源码中直接copy到项目中
如图:
当然,这里依旧给出代码:
VisibleRect.h
/********************************************* * * * 对界面区域常用坐标点的配置类 * * *********************************************/ #ifndef _VISIBLERECT_H_ #define _VISIBLERECT_H_ #include "cocos2d.h" USING_NS_CC; class VisibleRect { public: //获取可见区域 static Rect getVisibleRect(); //左边点 static Point left(); //右边点 static Point right(); //顶点 static Point top(); //底部点 static Point bottom(); //中心 static Point center(); //左上 static Point leftTop(); //右上 static Point rightTop(); //左下 static Point leftBottom(); //右下 static Point rightBottom(); private: //初始化 static void lazyInit(); //矩形 static Rect s_visibleRect; }; #endif /* __VISIBLERECT_H__ */
VisibleRect.cpp
/********************************************* * * * 对界面区域常用坐标点的配置类的实现 * * *********************************************/ #include "VisibleRect.h" USING_NS_CC; Rect VisibleRect::s_visibleRect; /** * 初始化函数,得到界面区域 */ void VisibleRect::lazyInit() { s_visibleRect = Director::getInstance()->getOpenGLView()->getVisibleRect(); } /** * 获取界面区域 */ Rect VisibleRect::getVisibleRect() { lazyInit(); return s_visibleRect; } /** * 获取左边坐标 */ Point VisibleRect::left() { lazyInit(); return Point(s_visibleRect.origin.x,s_visibleRect.origin.y+s_visibleRect.size.height/2); } /** * 获取右边坐标 */ Point VisibleRect::right() { lazyInit(); return Point(s_visibleRect.origin.x+s_visibleRect.size.width,s_visibleRect.origin.y+s_visibleRect.size.height/2); } /** * 获取顶部坐标 */ Point VisibleRect::top() { lazyInit(); return Point(s_visibleRect.origin.x+s_visibleRect.size.width/2,s_visibleRect.origin.y+s_visibleRect.size.height - 45); } /** * 获取底部坐标 */ Point VisibleRect::bottom() { lazyInit(); return Point(s_visibleRect.origin.x+s_visibleRect.size.width/2,s_visibleRect.origin.y); } /** * 获取中心点坐标 */ Point VisibleRect::center() { lazyInit(); return Point(s_visibleRect.origin.x+s_visibleRect.size.width/2,s_visibleRect.origin.y+s_visibleRect.size.height/2); } /** * 获取左上角坐标 */ Point VisibleRect::leftTop() { lazyInit(); return Point(s_visibleRect.origin.x,s_visibleRect.origin.y+s_visibleRect.size.height); } /** * 获取右上角坐标 */ Point VisibleRect::rightTop() { lazyInit(); return Point(s_visibleRect.origin.x+s_visibleRect.size.width,s_visibleRect.origin.y+s_visibleRect.size.height); } /** * 获取左下角坐标 */ Point VisibleRect::leftBottom() { lazyInit(); return s_visibleRect.origin; } /** * 获取右下角坐标 */ Point VisibleRect::rightBottom() { lazyInit(); return Point(s_visibleRect.origin.x+s_visibleRect.size.width,s_visibleRect.origin.y); }
至此,我们的项目结构如图:
下一节,我们将正式写界面的东西。