Cocos2d iphone 在CCRenderTexture上打洞

Cocos2d iphone 在CCRenderTexture上打洞,cocos2d-iphone,ccsprite,Cocos2d Iphone,Ccsprite,我正试图用CoCoS2D2.0在CCRenderTexture上打个洞 更具体地说,我有: 一个CCSprite“stars”,显示一些重复png图像的星星 除此之外,我还有一个CCRenderTexture“黑暗”,它完全覆盖了“星星”精灵 我希望能够在“黑暗”上切一个洞,以便显示下面的星星 我使用的是CCRenderTexture(如一些教程中所建议的),但我设法制作的洞从来都不是完全透明的,因此通过洞可见的星星部分被遮住了 我真的希望有人能告诉我真相,我花了一个多星期的时间在这件事上 代码

我正试图用CoCoS2D2.0在CCRenderTexture上打个洞

更具体地说,我有:

  • 一个CCSprite“stars”,显示一些重复png图像的星星
  • 除此之外,我还有一个CCRenderTexture“黑暗”,它完全覆盖了“星星”精灵
  • 我希望能够在“黑暗”上切一个洞,以便显示下面的星星

    我使用的是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
    

    我想你要找的是(可能需要一些编辑)


    至于你的代码,我想问题在于混合函数。查看它们是如何工作的。

    我认为您要查找的是(可能需要一些编辑)

    至于你的代码,我想问题在于混合函数。查看它们是如何工作的。

    谢谢。第一个链接(聚光灯演示)正是我想要的。我会看看代码,看看它是如何工作的。谢谢。第一个链接(聚光灯演示)正是我想要的。我将查看代码以了解它是如何工作的。