转自:http://blog.csdn.net/tangaowen/article/details/9023841
如果要在cocos2d-x中实现区域裁剪,一般要重写 visit函数:
void visit(void);
void MyControl::visit(void)
{
glEnable(GL_SCISSOR_TEST);
const float s = CCDirector::sharedDirector()->getContentScaleFactor();
CCPoint selfPos = this->getPosition();
CCSize selfContentSize = this->getContentSize();
CCLog("selfPos = %d,%d, selfContentSize =%d,%d ,s = %f", selfPos.x, selfPos.y ,selfContentSize.width, selfContentSize.height ,s);
// 如果是自己的锚点是(0.5,0.5)
// glScissor(selfPos.x *s - selfContentSize.width*s * 0.5f,
// selfPos.y * s - selfContentSize.height*s * 0.5f,
// selfContentSize.width*s,
// selfContentSize.height*s);
//自己的锚点是(0,0)
glScissor(selfPos.x *s ,
selfPos.y * s ,
selfContentSize.width*s,
selfContentSize.height*s);
CCNode::visit();//显示父类的内容
glDisable(GL_SCISSOR_TEST);
}
一般是这么写的,但是后面移植到安卓机器上面的时候,发现内容完全显示不出来了,查了些资料,改成如下的实现就好了:
void MyControl::visit(void)
{
glEnable(GL_SCISSOR_TEST);
CCPoint selfPos = this->getPosition();
CCSize selfContentSize = this->getContentSize();
CCLog("selfPos = %d,%d, selfContentSize =%d,%d ,s = %f", selfPos.x, selfPos.y ,selfContentSize.width, selfContentSize.height ,s);
float scaleX = CCEGLView::sharedOpenGLView()->getScaleX();
float scaleY = CCEGLView::sharedOpenGLView()->getScaleY();
CCRect viewPortRect = CCEGLView::sharedOpenGLView()->getViewPortRect();
glScissor(selfPos.x *scaleX + viewPortRect.origin.x ,
selfPos.y * scaleY + viewPortRect.origin.y ,
selfContentSize.width*scaleX,
selfContentSize.height*scaleY);
CCNode::visit();//显示父类的内容
glDisable(GL_SCISSOR_TEST);
}
如上,iOS跟 安卓系统都没有问题了,最好这样设置。