Opposite of glscissor in Cocos2D?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 06:49:59

问题


I found a class called ClippingNode that I can use on sprites to only display a specified rectangular area: https://github.com/njt1982/ClippingNode

One problem is that I need to do exactly the opposite, meaning I want the inverse of that. I want everything outside of the specified rectangle to be displayed, and everything inside to be taken out.

In my test I'm using a position of a sprite, which will update frame, so that will need to happen to meaning that new clipping rect will be defined.

CGRect menuBoundaryRect = CGRectMake(lightPuffClass.sprite.position.x, lightPuffClass.sprite.position.y, 100, 100);
ClippingNode *clipNode = [ClippingNode clippingNodeWithRect:menuBoundaryRect];

[clipNode addChild:darkMapSprite];
[self addChild:clipNode z:100];

I noticed the ClippingNode class allocs inside but I'm not using ARC (project too big and complex to update to ARC) so I'm wondering what and where I'll need to release too.

I've tried a couple of masking classes but whatever I mask fits over the entire sprite (my sprite covers the entire screen. Additionally the mask will need to move, so I thought glscissor would be a good alternative if I can get it to do the inverse.


回答1:


You don't need anything out of the box.

You have to define a CCClippingNode with a stencil, and then set it to be inverted, and you're done. I added a carrot sprite to show how to add sprites in the clipping node in order for it to be taken into account.

@implementation ClippingTestScene
{
    CCClippingNode *_clip;
}

And the implementation part

_clip = [[CCClippingNode alloc] initWithStencil:[CCSprite spriteWithImageNamed:@"white_board.png"]];
_clip.alphaThreshold = 1.0f;
_clip.inverted = YES;

_clip.position = ccp(self.boundingBox.size.width/2 , self.boundingBox.size.height/2);
[self addChild:_clip];

_img = [CCSprite spriteWithImageNamed:@"carrot.png"];
_img.position = ccp(-10.0f, 0.0f);
[_clip addChild:_img];

You have to set an extra flag for this to work though, but Cocos will spit out what you need to do in the console.




回答2:


I once used CCScissorNode.m from https://codeload.github.com/NoodlFroot/ClippingNode/zip/master

The implementation (not what you are looking for the inverse) was something :

    CGRect innerClippedLayer = CGRectMake(SCREENWIDTH/14, SCREENHEIGHT/6, 275, 325);

    CCScissorNode *tmpLayer = [CCScissorNode scissorNodeWithRect:innerClippedLayer];
    [self addChild:tmpLayer];

So for you it may be like if you know the area (rectangle area that you dont want to show i.e. inverse off) and you know the screen area then you can deduct the rectangle are from screen area. This would give you the inverse area. I have not did this. May be tomorrow i can post some code.



来源:https://stackoverflow.com/questions/23065425/opposite-of-glscissor-in-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!