- (void) beforeDraw{ glEnable(GL_SCISSOR_TEST); const CGFloat s = [[CCDirector sharedDirector] contentScaleFactor]; const int w = 90, h = 111; glScissor(self.position.x *s + w, self.position.y * s + h, self.contentSize.width*s - 2*w, self.contentSize.height*s - 2*h); } - (void) afterDraw{ glDisable(GL_SCISSOR_TEST); } - (void) visit{ // quick return if not visible if (!visible_) return; glPushMatrix(); if ( grid_ && grid_.active) { [grid_ beforeDraw]; [self transformAncestors]; } [self transform]; [self beforeDraw]; if(children_) { ccArray *arrayData = children_->data; NSUInteger i = 0; // draw children zOrder < 0 for( ; i < arrayData->num; i++ ) { CCNode *child = arrayData->arr[i]; if ( [child zOrder] < 0 ) [child visit]; else break; } // self draw [self draw]; // draw children zOrder >= 0 for( ; i < arrayData->num; i++ ) { CCNode *child = arrayData->arr[i]; [child visit]; } } else [self draw]; [self afterDraw]; if ( grid_ && grid_.active) [grid_ afterDraw:self]; glPopMatrix(); }
在CCLayer中,负责绘制的函数是在visit函数中,所以我们需要将这个函数重写。
修改- (void) beforeDraw中glScissor函数的参数即可改变显示的区域位置