require "extern" --这个类找到了可视区域的9个点的坐标:左上、上的中点、右上、左的中点、左下、下的中点、右下、右的中点。一般用于使用相对坐标的场合,解决自适应屏幕的问题 --改写自VisibleRect.cpp VisibleRect = class("VisibleRect") VisibleRect.__index = VisibleRect --CCRect VisibleRect::s_visibleRect; --创建一个CCRect实例:s_visibleRect(初始高、宽都是0) VisibleRect.s_visibleRect = CCRect:new() --[[ void VisibleRect::lazyInit() { if (s_visibleRect.size.width == 0.0f && s_visibleRect.size.height == 0.0f) { CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); s_visibleRect.origin = pEGLView->getVisibleOrigin(); s_visibleRect.size = pEGLView->getVisibleSize(); } } ]] function VisibleRect:lazyInit() if (self.s_visibleRect.size.width == 0.0 and self.s_visibleRect.size.height == 0.0) then --sharedOpenGLView:get the shared main open gl window local pEGLView = CCEGLView:sharedOpenGLView(); self.s_visibleRect.origin = pEGLView:getVisibleOrigin(); self.s_visibleRect.size = pEGLView:getVisibleSize(); end end --返回一个CCRect,就是可视区域 function VisibleRect:getVisibleRect() self:lazyInit(); -- return CCRectMake(s_visibleRect.origin.x, s_visibleRect.origin.y, s_visibleRect.size.width, s_visibleRect.size.height); return CCRectMake(self.s_visibleRect.origin.x, self.s_visibleRect.origin.y, self.s_visibleRect.size.width, self.s_visibleRect.size.height); --CCRectMake,输入坐标、长宽,输出一个CCRect end function VisibleRect:left() self:lazyInit(); return ccp(self.s_visibleRect.origin.x, self.s_visibleRect.origin.y+self.s_visibleRect.size.height/2); end function VisibleRect:right() self:lazyInit(); return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width, self.s_visibleRect.origin.y+self.s_visibleRect.size.height/2); end function VisibleRect:top() self:lazyInit(); return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width/2, self.s_visibleRect.origin.y+self.s_visibleRect.size.height); end function VisibleRect:bottom() self:lazyInit(); return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width/2, self.s_visibleRect.origin.y); end function VisibleRect:center() self:lazyInit(); return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width/2, self.s_visibleRect.origin.y+self.s_visibleRect.size.height/2); end function VisibleRect:leftTop() self:lazyInit(); return ccp(self.s_visibleRect.origin.x, self.s_visibleRect.origin.y+self.s_visibleRect.size.height); end function VisibleRect:rightTop() self:lazyInit(); return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width, self.s_visibleRect.origin.y+self.s_visibleRect.size.height); end function VisibleRect:leftBottom() self:lazyInit(); return self.s_visibleRect.origin; end function VisibleRect:rightBottom() self:lazyInit(); return ccp(self.s_visibleRect.origin.x+self.s_visibleRect.size.width, self.s_visibleRect.origin.y); end