(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook
著作权声明:本文由iam126 翻译,欢迎转载分享。
请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!
相关程序代码下载:http://download.csdn.net/detail/iam126/4068610
或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;
新手翻译,不准确请见谅,参考代码与原书。
1.14 使用AWTextureFilter实现毛边和字体阴影效果
学习了调用CCTexture2DMutable类的方式,AWTextureFilter类也可以用来创造一些不错的效果。 本章,我们将了解到高斯模糊,以及动态的阴影字体的创作。
如何工作…
执行如下代码:
#import "CCTexture2DMutable.h"
#import "AWTextureFilter.h"
@implementation Ch1_UsingAWTextureFilter
-(CCLayer*) runRecipe
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
//Pixel Format RGBA8888 is required for blur effects
//像素个事RGBA8888是模糊想过所需要的
[CCTexture2DsetDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
/*** Display a blurred texture ***/
//显示模糊材质
//Create the blur mutable texture
//创建一个可变的材质
CCTexture2DMutable *mutableBlurTexture = [[[CCTexture2DMutable alloc]initWithImage:[UIImage imageNamed:@"cocos2d_beginner.png"]]autorelease];
//Apply blur to the mutable texture
//在材质上添加模糊
[AWTextureFilter blur:mutableBlurTexture radius:3];
//Create a sprite to show the blur
//创建一个精灵来显示模糊
CCSprite *blurSprite = [CCSprite spriteWithTexture:mutableBlurTexture];
[blurSprite setPosition:ccp(winSize.width/2+blur.contentSize.width/2+1,winSize.height/2)];
//Add sprite to the scene
//加入到场景内
[self addChild:blurSprite z:0 tag:0];
/*** Display a selectively blurred texture ***/
//显示一个有可动的模糊材质
//Create the mutable texture to selectively blur
//为模糊创建一个可动的材质
CCTexture2DMutable *mutableSelectiveBlurTexture = [[[CCTexture2DMutablealloc] initWithImage:[UIImage imageNamed:@"cocos2d_beginner.png"]]autorelease];
//Apply selective blur to the mutable texture
//为这个可变材质添加模糊
[AWTextureFilter blur:mutableSelectiveBlurTexture radius:8rect:CGRectMake(240-200, (winSize.height-160)-75, 150, 150)];
//Create a sprite to show the selective blur
//创建一个精灵显示可移动的模糊效果
CCSprite *selectiveBlurSprite = [CCSpritespriteWithTexture:mutableSelectiveBlurTexture];
[selectiveBlurSprite setPosition:ccp(winSize.width/2,winSize.height/2)];
//Add sprite to the scene
//添加精灵
[self addChild:selectiveBlurSprite z:0 tag:1];
/*** Display dynamic font shadow effect ***/
//显示动态文字阴影效果
//Create a background so we can see the shadow
//创建一个背景让我们看清影子
CCLayerColor *background = [CCLayerColor layerWithColor:ccc4(200,100,100, 255) width:300 height:50];
[background setIsRelativeAnchorPoint:YES];
[background setAnchorPoint:ccp(0.5f, 0.5f)];
[background setPosition:ccp(winSize.width/2, winSize.height/2)];
//Create a sprite for the font label
//创建一个文字精灵
CCSprite* labelSprite = [CCSprite node];
[labelSprite setPosition:ccp(winSize.width/2, winSize.height/2)];
//Create a sprite for the shadow
//创建一个阴影精灵
CCSprite* shadowSprite = [CCSprite node];
[shadowSprite setPosition:ccp(winSize.width/2+1, winSize.height/2+1)];
//Color it black
//填黑色
[shadowSprite setColor:ccBLACK];
//Add sprites to a node and the node to the scene
//添加精灵到一个CCNode并添加进场景
CCNode* node = [[CCNode alloc] init];
[node addChild:background z:-1];
[node addChild:shadowSprite z:0];
[node addChild:labelSprite z:1];
[self addChild:node z:-1 tag:2];
//Create a mutable texture with a string
//创建一个阴影材质
CCTexture2DMutable *shadowTexture = [[[CCTexture2DMutable alloc]initWithString:@"Shadowed Text" fontName:@"Arial"fontSize:28] autorelease];
//Copy the mutable texture as non mutable texture
//复制这个材质给文字材质
CCTexture2D *labelTexture = [[shadowTexture copyMutable:NO]autorelease];
//Set the label texture
//设置材质
[labelSprite setTexture:labelTexture];
[labelSprite setTextureRect:CGRectMake(0, 0,shadowTexture.contentSize.width, shadowTexture.contentSize.height)];
//Apply blur to the shadow texture
//添加模糊给阴影材质
[AWTextureFilter blur:shadowTexture radius:4];
//Set the shadow texture
//设置阴影材质
[shadowSprite setTexture:shadowTexture];
[shadowSprite setTextureRect:CGRectMake(0, 0,shadowTexture.contentSize.width, shadowTexture.contentSize.height)];
return self;
}
如何工作…
AWTextureFilter使用CCTexture2DMutable类去得到一个高斯模糊的效果。这是一个操作复杂像素的例子。
文字阴影:
CCTexture2DMutable是CCTexture2D的子类。这允许我们使用这个方法:
-(id)initWithString:(NSString*)stringfontName:(NSString*)name fontSize:(CGFloat)size;
这个创建的文字纹理可以被用来创建一个模糊的文字阴影效果。通过创建一个相似的纹理并偏移、加深、模糊、最终绘制一个偏离中心的文字纹理。
更多的是…
对于模糊技术,这里有几个其他的建议:
1.模糊背景的快照作为暂停界面(可以看下一章的案例)。
2.把颜色效果和一个非常酷的光晕效果结合起来。
3.增大或减少模糊半径去适用一些猜谜类的游戏。
(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook
著作权声明:本文由iam126 翻译,欢迎转载分享。
请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!
相关程序代码下载:http://download.csdn.net/detail/iam126/4068610
或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;
新手翻译,不准确请见谅,参考代码与原书。