Cocos2d iphone Cocos2d iPhone-精灵剪辑/遮罩/框架

Cocos2d iphone Cocos2d iPhone-精灵剪辑/遮罩/框架,cocos2d-iphone,Cocos2d Iphone,如何在Cocos2D中剪裁/裁剪/遮罩或仅设置CCSprite的框架 类似于: 设置UIView的框架,剪裁子视图=TRUE 我的CCSprite主精灵添加了多个子精灵。 我只想要面具那主要的精灵的一部分可见。 有没有办法为CCSprite剪辑或使用遮罩 我可以剪切背景并在上面分层,只留下可见区域,但这是唯一的方法吗 下面是一个示例图像,演示了我试图实现的目标: (来源:)我最终使用了GL_剪刀 在MainSprite中,我提示: - (void) visit { if (!self.

如何在Cocos2D中剪裁/裁剪/遮罩或仅设置CCSprite的框架

类似于: 设置UIView的框架,剪裁子视图=TRUE

我的CCSprite主精灵添加了多个子精灵。 我只想要面具那主要的精灵的一部分可见。 有没有办法为CCSprite剪辑或使用遮罩

我可以剪切背景并在上面分层,只留下可见区域,但这是唯一的方法吗

下面是一个示例图像,演示了我试图实现的目标:

(来源:)

我最终使用了GL_剪刀

在MainSprite中,我提示:

- (void) visit
{
    if (!self.visible) {
        return;
    }
    glEnable(GL_SCISSOR_TEST);
    glScissor(x, y, width, height);   
    [super visit];
    glDisable(GL_SCISSOR_TEST);
}
这将剪裁或遮罩指定区域

唯一一个棘手的问题是,在场景模式中,COCOS2D在屏幕的左下角有0,0,而OpenGL则在右下角,因为它不考虑屏幕的方向。p>


换句话说,对于OpenGL,考虑到你有一个旋转的画像屏幕。

我写了一个剪辑节点类,它确实是这样的。您可以将其他节点(精灵、标签等)添加到ClippingNode,它们将仅在ClippingNode指定的区域中绘制。它还考虑了设备旋转

在内部,它像巴赫的答案一样使用了GL_剪刀测试


我尝试使用Steffen Itterheim的ClippingNode,但无法在足够健壮的环境下工作 足够满足我的需要

信不信由你,下面的代码运行得相当好,应该是完整的代码。它处理设备方向更改、锚点、位置、缩放(scaleX、scaleY)。对于cocos2dv2,您可能只需要 注释掉glPushMatrix和glPopMatrix调用

要使用,只需设置position和contentSize属性,并将要剪裁的子对象添加到此ClippingNode实例。contentSize属性用于定义剪裁区域的尺寸

example of usage:
ClippingNode *clipNode = [[ClippingNode alloc] init];
clipNode.anchorPoint = ccp(0.5f, 0);
clipNode.position = ccp(100, 25);
clipNode.contentSize = CGSizeMake(120, 120);

// add clipNode to your node hierarchy.
[parentNode addChild:clipNode];

// add one or more children to your clipNode:
[clipNode addChild:child1];

// ClippingNode.h
// CC0 - (public domain. Use in anyway you see fit.)
// No warranty of any kind is expressed or implied.
//
// by UChin Kim.
//
// the caller can simply set the regular cocos2d
// properties: position and contentSize to define the clipping region implicitly (i.e. the
// position and contentSize of the ClippingNode is the clipping region to be used).
// as an added bonus, position seems to work as expected (relative to parent node, instead of
// requiring absolute positioning).
//
// also, anchorPoint and scale properties seem to work as expected as well..
// no special code is neeed to handle device orientation changes correctly..
//
// To visually see exactly what is being clipped, set the following #define
// #define SHOW_CLIPPED_REGION_IN_LIGHT_RED 1
//

#import "cocos2d.h"

@interface ClippingNode : CCNode

@end

//
// ClippingNode.m
//
#import "ClippingNode.h"

@implementation ClippingNode

-(void) visit
{
CGPoint worldOrg = [self convertToWorldSpace:ccp(0, 0)];
CGPoint dest = [self convertToWorldSpace:ccp(self.contentSize.width, self.contentSize.height)];
CGPoint dims = ccpSub(dest, worldOrg);

glPushMatrix();
glEnable(GL_SCISSOR_TEST);

glScissor(worldOrg.x, worldOrg.y, dims.x, dims.y);

#if SHOW_CLIPPED_REGION_IN_LIGHT_RED
glColor4ub(64, 0, 0, 128);
ccDrawSolidRect(ccp(0, 0), ccp(1024, 1024));
#endif

[super visit];

glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}

@end

另一个警告-剪辑区域与精灵无关,它是基于世界的。因此,无论您如何变换精灵,它都将保持不变。如果你需要面具移动,你必须手动移动/缩放它和精灵。是的,你是对的。起初我没有意识到这一点。现在,我将x和y设置为相对于视图的位置,以便它们始终一起移动。@jtalarico,您知道在使用以下命令进行转换时从何处获取精灵变换:
CCDirector::sharedDirector()->pushScene(CCTransitionSlideInL::create(10.0f,pScene))谢谢!如何处理旋转剪裁节点?