Cocos2D 2.0 で CCRenderTexture に穴を開けようとしています。
より具体的には、私は持っています:
- いくつかの星が png 画像を繰り返していることを示す CCSprite の「星」。
- その上に、「星」スプライトを完全に覆う CCRenderTexture 「暗い」があります。
下の星を表示するために、「暗い」に穴を開けたいです。
私は CCRenderTexture を使用しています (いくつかのチュートリアルで提案されているように) が、作成した穴が完全に透明になることはないため、穴から見える星は部分的に隠されています。
誰かが私に光を見せてくれることを本当に願っています。私はこれに1週間以上費やしました...
これはコードです:
@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:¶ms];
[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