cocos2d 设置可见区域

本文介绍如何通过重写 visit 方法并利用 glScissor 函数实现 Cocos2d 中的可见区域裁剪功能。具体实现包括在 beforeDraw 方法中启用 GL_SCISSOR_TEST,并设置 scissor box 的位置和大小来限制绘图范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

coco2d 设置可见区域

参考http://blog.youkuaiyun.com/tangaowen/article/details/7539536

原理

1 重写 visit  方法

2 glScissor  函数

 

-(void) visit
{
    // quick return if not visible. children won't be drawn.
    if (!visible_)
        return;
    
    kmGLPushMatrix();
    
    if ( grid_ && grid_.active)
        [grid_ beforeDraw];
    
    [self beforeDraw];//绘制前
    
    [self transform];
    
    if(children_) {
        
        [self sortAllChildren];
        
        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];//绘制完成后
    
    // reset for next frame
    orderOfArrival_ = 0;
    
    if ( grid_ && grid_.active)
        [grid_ afterDraw:self];

    kmGLPopMatrix();
}
/**
 * clip this view so that outside of the visible bounds can be hidden.
 */
-(void)beforeDraw
{
    CGSize m_viewSize;
    m_viewSize.width = 300;
    m_viewSize.height = 200;
    
    glEnable(GL_SCISSOR_TEST);
    const CGFloat s = [[CCDirector sharedDirector] contentScaleFactor];
    glScissor(self.position.x *s - m_viewSize.width*s *0.5f,
              self.position.y * s - m_viewSize.height*s *0.5f,
              m_viewSize.width*s,
              m_viewSize.height*s);
      
}
/**
 * retract what's done in beforeDraw so that there's no side effect to
 * other nodes.
 */
-(void)afterDraw
{
    glDisable(GL_SCISSOR_TEST);
}

 

转载于:https://www.cnblogs.com/thc7/p/3295523.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值