我在这里使用剪切节点: http://www.learn-cocos2d.com/2011/01/cocos2d-gem-clippingnode ClippingNode.h #import "cocos2d.h"@interface ClippingNode : CCNode { CGRect clippingRegionInNodeCoordinates; CGRect clippingRegion;}@property (n
ClippingNode.h
#import "cocos2d.h" @interface ClippingNode : CCNode { CGRect clippingRegionInNodeCoordinates; CGRect clippingRegion; } @property (nonatomic) CGRect clippingRegion; @end
ClippingNode.m
#import "ClippingNode.h" @interface ClippingNode (PrivateMethods) -(void) deviceOrientationChanged:(NSNotification*)notification; @end @implementation ClippingNode -(CGRect) clippingRegion { return clippingRegionInNodeCoordinates; } -(void) setClippingRegion:(CGRect)region { // keep the original region coordinates in case the user wants them back unchanged clippingRegionInNodeCoordinates = region; self.position = clippingRegionInNodeCoordinates.origin; self.contentSize = clippingRegionInNodeCoordinates.size; // convert to retina coordinates if needed region = CC_RECT_POINTS_TO_PIXELS(region); // respect scaling clippingRegion = CGRectMake(region.origin.x * scaleX_, region.origin.y * scaleY_, region.size.width * scaleX_, region.size.height * scaleY_); } -(void) setScale:(float)newScale { [super setScale:newScale]; // re-adjust the clipping region according to the current scale factor [self setClippingRegion:clippingRegionInNodeCoordinates]; } -(void) deviceOrientationChanged:(NSNotification*)notification { // re-adjust the clipping region according to the current orientation [self setClippingRegion:clippingRegionInNodeCoordinates]; } -(void) visit { glEnable(GL_SCISSOR_TEST); CGPoint worldPosition = [self convertToWorldSpace:CGPointZero]; const CGFloat s = [[CCDirector sharedDirector] contentScaleFactor]; glScissor((clippingRegion.origin.x) + (worldPosition.x*s), (clippingRegion.origin.y) + (worldPosition.y*s), (clippingRegion.size.width), (clippingRegion.size.height)); [super visit]; glDisable(GL_SCISSOR_TEST); } @end
但是,我需要剪切一个旋转的CCNode.关于如何完成这项任务的任何想法?
由此替换ClippingNode类中的visit方法-(void) visit { float rotationAngle = 15; glPushMatrix(); CCRenderTexture* renderTexture = [[CCRenderTexture renderTextureWithWidth:512 height:512] retain]; glEnable(GL_SCISSOR_TEST); glScissor(0, 0, clippingRegion.size.width, clippingRegion.size.height); [renderTexture begin]; glPushMatrix(); glRotatef(rotationAngle, 0, 0, 1); glTranslatef(-clippingRegion.origin.x, -clippingRegion.origin.y, 0); [super visit]; glPopMatrix(); [renderTexture end]; glDisable(GL_SCISSOR_TEST); renderTexture.sprite.position = CGPointMake(clippingRegion.origin.x , clippingRegion.origin.y); renderTexture.sprite.anchorPoint = CGPointMake(0, 1); renderTexture.sprite.rotation = rotationAngle; [renderTexture.sprite visit]; [renderTexture release]; glPopMatrix(); }
基本上它创建了一个纹理,用于呈现ClippingNode内容
然后翻译场景,使裁剪区域中的原点现在为(0,0)
通过rotationAngle旋转整个场景
启用剪刀
渲染场景
翻译,旋转和渲染包含纹理的精灵