如何在Cocos2D 1.0 中掩饰一个精灵(六)
发布时间:2020-12-14 16:44:46 所属栏目:百科 来源:网络整理
导读:大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰一个精灵:实现代码 打开HelloWorldLayer.m并且在init方法上方添加如下方法: - (CCSprite *)maskedSpriteWithSprite:(CCSpri
掩饰一个精灵:实现代码 打开HelloWorldLayer.m并且在init方法上方添加如下方法: - (CCSprite *)maskedSpriteWithSprite:(CCSprite *)textureSprite maskSprite:(CCSprite *)maskSprite {
// 1
CCRenderTexture * rt = [CCRenderTexture renderTextureWithWidth:maskSprite.contentSizeInPixels.width height:maskSprite.contentSizeInPixels.height];
// 2
maskSprite.position = ccp(maskSprite.contentSize.width/2,maskSprite.contentSize.height/2);
textureSprite.position = ccp(textureSprite.contentSize.width/2,textureSprite.contentSize.height/2);
// 3
[maskSprite setBlendFunc:(ccBlendFunc){GL_ONE,GL_ZERO}];
[textureSprite setBlendFunc:(ccBlendFunc){GL_DST_ALPHA,GL_ZERO}];
// 4
[rt begin];
[maskSprite visit];
[textureSprite visit];
[rt end];
// 5
CCSprite *retval = [CCSprite spriteWithTexture:rt.sprite.texture];
retval.flipY = YES;
return retval;
}
让我们一段一段的看:
在我们讨论的所有东西之后,现在让我们来实际使用它!用一下代码替换BEGINTEMP和ENDTEMP之间的代码: CCSprite * mask = [CCSprite spriteWithFile:@"CalendarMask.png"];
CCSprite * maskedCal = [self maskedSpriteWithSprite:cal maskSprite:mask];
maskedCal.position = ccp(winSize.width/2,winSize.height/2);
[self addChild:maskedCal];
该代码用我们新的函数去掩饰日历精灵,并且添加掩饰后的版本到场景中. 编译运行,你应该看到一个被Cocos2D 1.0掩饰后的精灵! 关于CCRenderTexture方法的缺点 对于这个简单的例子app来说工作的不错,但是这里该方法有一些缺点你可能会在更复杂的app中注意到:
就像我前面提到的那样,在OpenGL ES 1.0中据我所知没有办法绕过这些缺点.但在OpenGL ES 2.0中你可以通过着色器掩饰的更有效率 — 但是那是另一段旅程的主题了! ;) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |