카테고리 없음
[iOS] cocos2d 3으로 원 안에 스프라이트 마스크
행복을전해요
2021. 1. 28. 20:18
CCSprite에서 이미지로 원 스프라이트를 직접 설정할 수 없습니다. 보기를 가져와 CCScene에 추가합니다.
먼저 AGMedallionView 클래스 를 다운로드 하고 프로젝트에 추가합니다.
장면에서 해당 클래스를 가져온 후
#import "AGMedallionView.h"
장면의 초기화 방법
-(id) init
{
if( (self=[super init]) )
{
AGMedallionView *view1 = [[AGMedallionView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[view1 setImage:[UIImage imageNamed:@"Icon-Small@2x.png"]];
view1.borderColor = [UIColor redColor];
view1.borderWidth = 5.0;
[[[CCDirector sharedDirector] view] addSubview:view1];
}
}
아래와 같이 표시
초기 작업 덕분에 다음과 같이 풍경 배경 위에 그라디언트를 넣을 수있었습니다.
CCSprite* spriteStencil = [CCSprite spriteWithImageNamed:stringSceneryfilename];
spriteStencil.anchorPoint = ccp(0, 0);
CCClippingNode* crop = [CCClippingNode clippingNodeWithStencil:spriteStencil];
crop.alphaThreshold = 0;
CCNodeGradient* nodegradient = [CCNodeGradient nodeWithColor:[CCColor colorWithRed:0 green:0 blue:0 alpha:0] fadingTo:[CCColor colorWithRed:0 green:0 blue:0 alpha:1] alongVector:ccp(0, 1)];
nodegradient.contentSizeInPoints = CGSizeMake(spriteScenery1.contentSizeInPoints.width, WINSIZE.height);
nodegradient.anchorPoint = ccp(0, 0);
[crop addChild:nodegradient];
[spriteScenery1 addChild:crop];
그라디언트는 풍경 파일의 알파가 0 이상인 풍경 만 덮습니다. 풍경에는 완전히 투명한 영역 (하늘 ...)이 많이 있습니다. 임시 생성 그라디언트 (CCNodeGradient)를 사용하고 있으므로 필요하지 않습니다. 잘릴 CCSprite를 정확히 사용합니다. 제 경우에는 그래디언트를 자르고 원본 이미지 위에 그래디언트를 넣습니다 (스텐실과 동일한 이미지 소스입니다!).
물론 나는 또한 사용
[cocos2dSetup setObject:@GL_DEPTH24_STENCIL8_OES forKey:CCSetupDepthFormat]; // for stencils to work? aka CCClippingNode
앱 델리게이트에서.
출처
https://stackoverflow.com/questions/22039787