Cocos2d iPhone CCClippingNode不做剪辑

时间:2013-09-24 12:07:11

标签: cocos2d-iphone clipping

以下是我的代码,我试图在其上绘制图像和一个洞。 (代码来自cocos2d测试项目)

    CCSprite *target = [CCSprite spriteWithFile:@"blocks.png"];
    target.anchorPoint = CGPointZero;
    target.scale = 3;

    CCClippingNode *outerClipper_ = [[CCClippingNode clippingNode] retain];
    outerClipper_.contentSize = CGSizeApplyAffineTransform(target.contentSize, CGAffineTransformMakeScale(target.scale, target.scale));
    outerClipper_.anchorPoint = ccp(0.5, 0.5);
    outerClipper_.position = ccpMult(ccpFromSize([CCDirector sharedDirector].winSize), 0.5);

    outerClipper_.stencil = target;

    CCClippingNode *holesClipper = [CCClippingNode clippingNode];
    holesClipper.inverted = YES;
    holesClipper.alphaThreshold = 0.05;

    [holesClipper addChild:target];

    CCNode *holes_ = [[CCNode node] retain];

    [holesClipper addChild:holes_];

    CCNode *holesStencil_ = [[CCNode node] retain];

    holesClipper.stencil = holesStencil_;

    [outerClipper_ addChild:holesClipper];

    [self addChild:outerClipper_ z:9999];


// Add the hole

    CCSprite *hole = [CCSprite spriteWithFile:@"hole_effect.png"];
    hole.position = [outerClipper_ convertToNodeSpace:ccpMult(ccpFromSize([CCDirector sharedDirector].winSize), 0.5)];

    [holes_ addChild:hole];

    CCSprite *holeStencil = [CCSprite spriteWithFile:@"hole_stencil.png"];
    holeStencil.position = [outerClipper_ convertToNodeSpace:ccpMult(ccpFromSize([CCDirector sharedDirector].winSize), 0.5)];

    [holesStencil_ addChild:holeStencil];

所有图像都可以在cocos2d测试项目中找到。

问题是图像出现了,但它上面没有洞。我做错了什么?

1 个答案:

答案 0 :(得分:1)

问题是我没有正确设置CCGLView。我必须将深度格式设置为GL_DEPTH24_STENCIL8_OES而不是值0.