Search code examples
ioscocos2d-iphoneclippingspritebuilder

Clip CCSprite/CCNode with another CCSprite/CCNode - Cocos2D


I can clip using native coding within iOS however, as I wish to port across to Android using SpriteBuilder I want to clip 2 CCSprites using Cocos2D.

I am looking to do the following :

enter image description here

I have seen libraries which support only Cocos2D 2 however I am using the latest version and these no longer seem to work.

How would I achieve this affect?


Solution

  • For anyone looking for a similar fix the following is native to Cocos2D.

    //Get Screen size
        CGSize winSize = [[CCDirector sharedDirector] viewSize];
    
    //Set Clipping Sprite
        CCSprite *heroClip = [CCSprite spriteWithImageNamed:@"stamina/MenuHappinessWhite.png"];
        heroClip.position = ccp(winSize.width/2, winSize.height/2); // Middle of screen
    
    //Set Sprite below Clipping
        CCSprite *heroUnder = [CCSprite spriteWithImageNamed:@"stamina/MenuLevel.png"];
        heroUnder.position = ccp(winSize.width/2, winSize.height/2);
        heroUnder.scaleY = 0.5f;
    
        // Create Clipping Node
        CCClippingNode *scissor = [CCClippingNode clippingNodeWithStencil:heroClip];
        [scissor setContentSize:self.contentSize];
        [scissor setPositionType:CCPositionTypeNormalized];
    
        [scissor setAlphaThreshold:0.0];
    
        //[scissor setInverted:YES];
        [self addChild:scissor];
    
        // Add nodes to Clipping Node
        [scissor addChild:heroUnder];
    

    and add this to app delegate

       //Load Clipping Mask
        [cocos2dSetup setObject:@GL_DEPTH24_STENCIL8_OES forKey:CCSetupDepthFormat];