2012-12-15 4 views
0

Cocender2D 2.0으로 CCRenderTexture에 구멍을 만들려고합니다.CCRenderTexture에 구멍 만들기

은 더 구체적으로 내가 가진 : PNG 파일 이미지를 반복 일부 별을 보여줍니다

  1. CCSprite "별";
  2. 나는 "별"스프라이트를 완전히 덮는 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 

답변

3

: 난 정말 어떤 사람이 나에게 빛을 보여줄 수 있기를 바랍니다

, 나는

코드입니다 ...이에 일주일 동안 지출 this (일부 수정이 필요할 수 있음)과 같은 것입니다.

코드의 경우 문제는 블렌드 함수에 있습니다. 그들이 어떻게 작동하는지 보려면 this을 확인하십시오.

+0

감사합니다. 첫 번째 링크 (스포트라이트 데모)는 내가 찾고있는 것입니다. 코드가 어떻게 작동하는지 알아볼 것입니다. –