Cocos2d iPhone - 精灵剪辑/蒙版/框架剪辑、框架、精灵、Cocos2d

2023-09-06 14:46:29 作者:不懂我的寂寞

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

how can i clip/crop/mask or just set the frame of a CCSprite in Cocos2D?

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

Something similar to: setting the frame for UIView, with clipping subviews = TRUE

我的 CCSprite Main Sprite 添加了多个 Child Sprite.我只希望 Main Sprite Sprite 的 Mask 部分可见.有没有办法为 CCSprite 剪辑或使用蒙版?

My CCSprite Main Sprite have multiple Child Sprite added to it. I only want Mask part of that Main Sprite Sprite visible. Is there a way to clip or use a mask for CCSprite?

我可以剪掉背景和上面的图层,只留下可见区域,但这是唯一的方法吗?!

I could cut the background and layer that on top, leaving only that visible area, but is that the only way?!

这是一个示例图片,展示了我想要实现的目标:(来源:dnamique.com)

here's a sample image demonstrating what I'm trying to achieve: (source: dnamique.com)

推荐答案

我最终使用了 GL_SCISSOR.

I ended up using GL_SCISSOR.

在我实现的 MainSprite 中:

in MainSprite I impemented:

- (void) visit
{
    if (!self.visible) {
        return;
    }
    glEnable(GL_SCISSOR_TEST);
    glScissor(x, y, width, height);   
    [super visit];
    glDisable(GL_SCISSOR_TEST);
}

这将剪切或遮盖指定区域.

This will clip or mask the specified area.

唯一棘手的一点是,在横向模式下,Cocos2D 在屏幕的左下角有 0,0,而 OpenGL 在右下角有它,因为它不考虑屏幕的方向.

The only tricky bit is that in Landscape mode Cocos2D has 0,0 at the bottom-left side of the screen, while OpenGL has it at the bottom-right corner as it doesn't consider the orientation of the screen.

换句话说,对于 OpenGL,假设您有一个旋转的纵向屏幕.

In other words, for OpenGL consider you have a rotated portrait Screen.