I've been trying to modify Steve Oldmeadow's scratch off example so that the "scratch off" area is an image (CCSprite) instead of the color back. Steve used a CCRenderTexture and the fire image as a sprite to remove the alpha as you paint thus showing the background underneath.
Using the code bellow I was expecting to scratch away the scratchBg (yellow image) to see the background (purple image) bellow however I just see white for some reasons.
Here's what I have:
- bg = back ground image (purple block 480x320)
- scratchBg = the image (yellow block 480x320) I want "scratch away". In the original example it is just solid black.
- burnSprite = fire.png which is being used as the brush.
Here's what I get: result
Any help would be appreciated.
-(id) init
{
if( (self=[super init] )) {
CGSize size = [[CCDirector sharedDirector] winSize];
[self setIsTouchEnabled:YES];
reset = YES;
bg = [CCSprite spriteWithFile:@"background.png"];
bg.position = ccp( size.width /2 , size.height/2 );
[self addChild:bg z:1];
//Set up the burn sprite that will "knock out" parts of the darkness layer depending on the
//alpha value of the pixels in the image.
burnSprite = [CCSprite spriteWithFile:@"fire.png"];
[burnSprite setBlendFunc: (ccBlendFunc) { GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }];
[burnSprite retain];
// Scratch Background
scratchBg = [[CCSprite spriteWithFile:@"scratchBackground.png"] retain];
scratchBg.position = ccp(size.width / 2, size.height / 2);
// Scratch Layer
scratchLayer = [CCRenderTexture renderTextureWithWidth:size.width height:size.height];
scratchLayer.position = ccp(size.width / 2 , size.height / 2);
[self addChild:scratchLayer z:2];
[self schedule: @selector(tick:)];
}
return self;
}
//Move the burn sprite unless user double taps - then we reset the darkness layer
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch tapCount] >= 2) {
reset = YES;
} else {
CGPoint location = [touch locationInView: [touch view]];
burnSprite.position = CGPointMake(location.y, location.x);
}
}
//Move the burn sprite
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
burnSprite.position = CGPointMake(location.y, location.x);
}
-(void) tick: (ccTime) dt {
if (reset) {
[scratchLayer clear:0.0f g:0.0f b:0.0f a:1.0f];
[scratchLayer begin];
// Redraw scratch background
[scratchBg visit];
[scratchLayer end];
reset = NO;
}
// Update the render texture
[scratchLayer begin];
// Limit drawing to the alpha channel
glColorMask(0.0f, 0.0f, 0.0f, 1.0f);
// Draw
[burnSprite visit];
// Reset color mask
glColorMask(1.0f, 1.0f, 1.0f, 1.0f);
[scratchLayer end];
}
Cheers
Colin