(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook
著作权声明:本文由iam126 翻译,欢迎转载分享。
请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!
相关程序代码下载:http://download.csdn.net/detail/iam126/4068610
或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;
新手翻译,不准确请见谅,参考代码与原书。
1.6网格,粒子,追尾特效
cocos2d配备了一个多样且易于掌握的特效系统。我们将简要的介绍这些简单的广泛使用的特效。
如何工作…
为了得到网格效果,首先你需要设定EAGLView pixelFormat 属性为kEAGLColorFormatRGBA8(默认的时候是kEAGLColorFormatRGB565).
这些工作都在项目文件中的AppDelegate.m文件中改动,下面是代码:
EAGLView*glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGB565
depthFormat:0
];
改成这样:
EAGLView*glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:0
];
然后,执行如下代码:
//Custom particle effect
//自定义粒子效果
@implementation ParticleWaterfall
-(id)init
{
return [self initWithTotalParticles:400];
}
-(id)initWithTotalParticles:(int)p
{
if(self != [super initWithTotalParticles: p])
{
return nil;
}
//Angle
//角度
angle = 270;
angleVar = 12;
//Emitter position
//发射器位置
self.position = ccp(160, 60);
posVar = ccp(16, 4);
//Life of particles
//粒子生命
life = 2;
lifeVar = 0.25f;
//Speed of particles
//粒子发散速度
self.speed = 100;
self.speedVar = 20;
self.gravity = ccp(self.gravity.x, -5);
//Size of particles
//粒子大小
startSize = 35.0f;
endSize = 100.0f;
//Color of particles
//粒子颜色
startColor = ccc4(0.4f, 0.4f, 1.0f, 0.6f);
startColorVar = ccc4(0,0,0,0);
endColor = ccc4(0.5f, 0.5f, 0.5f, 0);
endColorVar = ccc4(0,0,0,0);
//Additive
//添加模式?
self.blendAdditive = NO;
return self;
}
@end
@interface Ch1_GridParticleMotionEffects
{
//Variables for motion streak effect
//拖尾效果
CCSprite *rocket;
CCMotionStreak *streak;
CGPoint rocketDirection;
}
@implementationCh1_GridParticleMotionEffects
-(CCLayer*) runRecipe
{
CGSize s = [[CCDirector sharedDirector] winSize];
/*** Grid effect demo ***/
//网格效果例子
//Create a CCSprite
//创建精灵
CCSprite *sprite = [CCSpritespriteWithFile:@"colorable_sprite.png"];
[sprite setPosition:ccp(240,160)];
[self addChild:sprite z:1 tag:TAG_SPRITE];
//Create a grid effect
//创建网格效果
CCAction *gridEffect = [CCShaky3D actionWithRange:5 shakeZ:YESgrid:ccg(15,10) duration:10];
//Run the effect
//运行该效果
[sprite runAction:gridEffect];
/*** Particle effect demo ***/
//粒子效果
//Create a simple fire particle effect
//创建一个简单的火焰效果
CCNode *fireEffect = [CCParticleFire node];
[self addChild:fireEffect z:1 tag:TAG_FIRE_EFFECT];
//Create a waterfall particle effect
//创建一个瀑布效果
CCNode *waterfallEffect = [ParticleWaterfall node];
[self addChild:waterfallEffect z:1 tag:TAG_WATERFALL_EFFECT];
/*** Motion streak demo ***/
//追尾
//Set the rocket initially in a random direction.
//设置随即方向
rocketDirection = ccp(arc4random()%4+1,arc4random()%4+1);
//Add the rocket sprite.
//添加精灵
rocket = [CCSprite spriteWithFile:@"rocket.png"];
[rocket setPosition:ccp(s.width/2, s.height/2)];
[rocket setScale:0.5f];
[self addChild:rocket];
//Create the streak object and add it to the scene.
//创建追尾对象并加入到场景中
streak = [CCMotionStreak streakWithFade:1 minSeg:1image:@"streak.png" width:32 length:32 color:ccc4(255,255,255,255)];
[self addChild:streak];
streak.position = ccp(s.width/2, s.height/2);
[self schedule:@selector(step:)];
return self;
}
-(void)step:(ccTime)delta
{
CGSize s = [[CCDirector sharedDirector] winSize];
//Make rocket bounce off walls
//遇到边框弹回
if(rocket.position.x > s.width || rocket.position.x < 0)
{
rocketDirection = ccp(-rocketDirection.x, rocketDirection.y);
}
else if(rocket.position.y > s.height || rocket.position.y < 0)
{
rocketDirection = ccp(rocketDirection.x, -rocketDirection.y);
}
//Slowly turn the rocket
//漫漫上升
rocketDirection = ccp(rocketDirection.x, rocketDirection.y+0.05f);
//Update rocket position based on direction
//更新位置
rocket.position = ccp(rocket.position.x + rocketDirection.x,rocket.position.y + rocketDirection.y);
[streak setPosition:rocket.position];
//Set the rocket's rotation
//设置旋转值
[rocket setRotation:radiansToDegrees(vectorToRadians(rocketDirection))];
}
@end
如何工作…
在这个案例中我们了解了一些东西。为了简洁,我们只包括了一种网格效果以及两种粒子效果。每一个效果都在RecipeCollection01展示了,包括一些自定义的特效比如Waterfall(瀑布)和WaterSplash(水花)。
自定义粒子:
cocos2d粒子的种类很多,并且非常利于你自己创建粒子类。下面是一些cocos2d内部的粒子类:
CCParticleExplosion, CCParticleFire, CCParticleFireworks,
CCParticleFlower, CCParticleGalaxy, CCParticleMeteor,
CCParticleRain, CCParticleSmoke, CCParticleSnow,
CCParticleSpiral, CCParticleSun.
使用CCMotionStreak:
追尾特效是CCNode中的一种非常好的动态元素表达方式。通常和一些粒子效果搭配会产生良好的效果。
要注意的是,当创建一个追尾效果的时候,最好找一张在弯曲情况下仍然好看的纹理贴图。
一般情况下,带有透明渐变的纹理效果都不错~
(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook
著作权声明:本文由iam126 翻译,欢迎转载分享。
请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!
相关程序代码下载:http://download.csdn.net/detail/iam126/4068610
或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;
新手翻译,不准确请见谅,参考代码与原书。