我有一个图像和一个形状。我需要使用我的形状的alpha通道从图像创建一个剪切,这就是我的意思:
我可以使用CCMask,但结果并没有给我留下深刻的印象。 你能指点一个替代解决方案吗? 非常感谢任何帮助。
答案 0 :(得分:1)
看一下这些教程,
Cocos2d 2.0: http://www.raywenderlich.com/4428/how-to-mask-a-sprite-with-cocos2d-2-0
Cocos2d 1.0: http://www.raywenderlich.com/4421/how-to-mask-a-sprite-with-cocos2d-1-0
答案 1 :(得分:0)
<强> - =(W)= - 强>
我遇到了类似的问题。我阅读了教程并使用了CCMask。 CCMask是一个很好的类,但有一个问题。当面具的大小大于纹理的大小时,我们可以看到面具的碎片,这是非常糟糕的。 使用我的决定并享受!
-(CCSprite *)maskedSpriteWithSprite:(CCSprite *)textureSprite maskSprite:(CCSprite *)maskSprite
{
CCSprite *maskSprite_ = [CCSprite spriteWithTexture:[maskSprite texture]];
CCSprite *textureSprite_ = [CCSprite spriteWithTexture:[textureSprite texture]];
maskSprite_.position = maskSprite.position;
maskSprite_.rotation = maskSprite.rotation;
maskSprite_.opacity = maskSprite.opacity;
maskSprite_.scale = maskSprite.scale;
maskSprite_.zOrder = maskSprite.zOrder;
textureSprite_.position = textureSprite.position;
textureSprite_.rotation = textureSprite.rotation;
textureSprite_.opacity = textureSprite.opacity;
textureSprite_.scale = textureSprite.scale;
textureSprite_.zOrder = textureSprite.zOrder;
[maskSprite_ setColor:ccc3(0.0, 0.0, 0.0)];
//Start cutting sprite
CCRenderTexture *rt;
float dx;
float dy;
//Correct mask
rt = [CCRenderTexture renderTextureWithWidth:maskSprite_.contentSize.width*maskSprite_.scale height:maskSprite_.contentSize.height*maskSprite_.scale];
dx = textureSprite_.position.x - maskSprite_.position.x;
dy = textureSprite_.position.y - maskSprite_.position.y;
maskSprite_.position = ccp(maskSprite_.contentSize.width/2*maskSprite_.scale, maskSprite_.contentSize.height/2*maskSprite_.scale);
textureSprite_.position = ccp(maskSprite_.position.x+dx, maskSprite_.position.y+dy);
[textureSprite_ setBlendFunc:(ccBlendFunc){GL_ONE, GL_ZERO}];
[maskSprite_ setBlendFunc:(ccBlendFunc){GL_DST_ALPHA, GL_ZERO}];
[rt begin];
[textureSprite_ visit];
[maskSprite_ visit];
[rt end];
CCSprite *correctMaskSprite_ = [CCSprite spriteWithTexture:rt.sprite.texture];
correctMaskSprite_.flipY = YES;
correctMaskSprite_.position = maskSprite.position;
correctMaskSprite_.rotation = maskSprite.rotation;
correctMaskSprite_.opacity = maskSprite.opacity;
correctMaskSprite_.scale = maskSprite.scale;
correctMaskSprite_.zOrder = maskSprite.zOrder;
//New sprite with correct mask
[correctMaskSprite_ setBlendFunc:(ccBlendFunc){GL_ONE, GL_ZERO}];
[textureSprite_ setBlendFunc:(ccBlendFunc){GL_DST_ALPHA, GL_ZERO}];
[rt begin];
[correctMaskSprite_ visit];
[textureSprite_ visit];
[rt end];
CCSprite *retval = [CCSprite spriteWithTexture:rt.sprite.texture];
retval.flipY = YES;
return retval;
}