代码之家  ›  专栏  ›  技术社区  ›  Bach

Cocos2d iPhone-精灵剪辑/面具/框架

  •  14
  • Bach  · 技术社区  · 16 年前

    类似于: 设置UIView的框架,其中clipping subviews=TRUE

    主精灵 有多个 加上。 面具 这是主要原因之一 雪碧雪碧 可见。 有没有办法剪辑或使用CCSprite的面具?

    我可以把背景剪掉,在上面分层,只留下可见区域,但这是唯一的方法吗?!

    下面是一个示例图像,演示了我要实现的目标: alt text
    (来源: dnamique.com

    3 回复  |  直到 7 年前
        1
  •  15
  •   Bach    16 年前

    最后我用了一把剪刀。

    在MainSprite中,我建议:

    - (void) visit
    {
        if (!self.visible) {
            return;
        }
        glEnable(GL_SCISSOR_TEST);
        glScissor(x, y, width, height);   
        [super visit];
        glDisable(GL_SCISSOR_TEST);
    }
    

    这将剪裁或遮罩指定区域。

    唯一棘手的是,在横向模式下,Cocos2D在屏幕的左下角有0,0,而OpenGL在右下角有0,0,因为它不考虑屏幕的方向。

    换言之,对于OpenGL,请考虑您有一个旋转的纵向屏幕。

        2
  •  11
  •   CodeSmile Paul Beusterien    15 年前

    我写了一个ClippingNode类就是这样做的。您可以将其他节点(精灵、标签等)添加到ClippingNode,它们将仅在ClippingNode指定的区域中绘制。它还考虑了设备的旋转。

    在内部,它使用了像巴赫的答案一样的GL\u剪刀测试。

    http://www.learn-cocos2d.com/2011/01/cocos2d-gem-clippingnode/

        3
  •  3
  •   Gino    13 年前

    够我穿的了。

    注释掉glPushMatrix和glPopMatrix调用。。

    要使用,只需设置position和contentSize属性,并将要剪裁的子对象添加到此ClippingNode实例。contentSize属性用于定义剪裁区域的尺寸。

    example of usage:
    ClippingNode *clipNode = [[ClippingNode alloc] init];
    clipNode.anchorPoint = ccp(0.5f, 0);
    clipNode.position = ccp(100, 25);
    clipNode.contentSize = CGSizeMake(120, 120);
    
    // add clipNode to your node hierarchy.
    [parentNode addChild:clipNode];
    
    // add one or more children to your clipNode:
    [clipNode addChild:child1];
    
    // ClippingNode.h
    // CC0 - (public domain. Use in anyway you see fit.)
    // No warranty of any kind is expressed or implied.
    //
    // by UChin Kim.
    //
    // the caller can simply set the regular cocos2d
    // properties: position and contentSize to define the clipping region implicitly (i.e. the
    // position and contentSize of the ClippingNode is the clipping region to be used).
    // as an added bonus, position seems to work as expected (relative to parent node, instead of
    // requiring absolute positioning).
    //
    // also, anchorPoint and scale properties seem to work as expected as well..
    // no special code is neeed to handle device orientation changes correctly..
    //
    // To visually see exactly what is being clipped, set the following #define
    // #define SHOW_CLIPPED_REGION_IN_LIGHT_RED 1
    //
    
    #import "cocos2d.h"
    
    @interface ClippingNode : CCNode
    
    @end
    
    //
    // ClippingNode.m
    //
    #import "ClippingNode.h"
    
    @implementation ClippingNode
    
    -(void) visit
    {
    CGPoint worldOrg = [self convertToWorldSpace:ccp(0, 0)];
    CGPoint dest = [self convertToWorldSpace:ccp(self.contentSize.width, self.contentSize.height)];
    CGPoint dims = ccpSub(dest, worldOrg);
    
    glPushMatrix();
    glEnable(GL_SCISSOR_TEST);
    
    glScissor(worldOrg.x, worldOrg.y, dims.x, dims.y);
    
    #if SHOW_CLIPPED_REGION_IN_LIGHT_RED
    glColor4ub(64, 0, 0, 128);
    ccDrawSolidRect(ccp(0, 0), ccp(1024, 1024));
    #endif
    
    [super visit];
    
    glDisable(GL_SCISSOR_TEST);
    glPopMatrix();
    }
    
    @end
    
    推荐文章