在CCRenderTexture上打个洞

时间:2012-12-15 17:20:02

标签: cocos2d-iphone ccsprite

我正试图用Cocos2D 2.0在CCRenderTexture上打个洞。

更具体地说,我有:

  1. 一个CCSprite“星星”,显示一些重复png图像的星星;
  2. 最重要的是,我有一个完全覆盖“星星”精灵的CCRenderTexture“黑暗”。
  3. 我希望能够在“黑暗”上打一个洞以显示下面的星星。

    我正在使用CCRenderTexture(正如一些教程中所建议的那样),但我设法使的洞永远不会完全透明,因此通过这个洞可见的星星部分被遮挡。

    我真的希望有人可以向我展示光明,我花了一个多星期的时间......

    这是代码:

    @interface MyBackgroundLayer : CCLayerGradient {
    }
    @end
    
    @implementation MyBackgroundLayer
    CCRenderTexture * dark;
    CCSprite * stars;
    
    -(id) init
    {
    if (self=[super init]) {
        CGSize size = [[CCDirector sharedDirector] winSize];
    
        // background
        stars = [CCSprite spriteWithFile:@"stars.png" rect:CGRectMake(0, 0, size.width, size.height)];
        stars.anchorPoint = ccp(0,0);
        ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
        [stars.texture setTexParameters:&params];
        [self addChild:stars];
    
        // dark layer to cover the background
        dark = [CCRenderTexture renderTextureWithWidth:size.width height:size.height];
        [dark clear:0 g:0 b:0 a:1.0f];
        [self addChild: dark];
        dark.position = ccp(size.width/2,size.height/2);
        [[dark sprite] setBlendFunc: (ccBlendFunc) { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA }];
        }
    
        return self;
    }
    
    -(void)draw
    {
        [super draw];
        [dark beginWithClear:0 g:0 b:0 a:1];
        glColorMask(0, 0, 0, 1);
    
        // Here I am using 0.5 as alpha value, this could seems the reason why the hole is not fully transparent.
        // However if I change the alpha value to 1.0f or 0.0f the hole becomes completely opaque
        ccColor4F color = ccc4f(1.f, 1.f, 1.f, 0.5f); 
        ccDrawSolidRect(ccp(0,0), ccp(600, 600), color);
    
        glColorMask(1,1,1,1);
        [dark end];
    }
    @end
    

1 个答案:

答案 0 :(得分:3)

我认为您正在寻找的是this(可能需要进行一些编辑)。

至于你的代码..i问题在于混合功能。检查this,看看它们是如何工作的。