(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.12使用层来交换调色板

(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook

著作声明:本文由iam126 翻译,欢迎转载分享。

请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!

相关程序代码下载:http://download.csdn.net/detail/iam126/4068610

或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;

新手翻译,不准确请见谅,参考代码与原书。

 

1.12使用层来交换调色板

 

很多游戏开发者都以一个重要的工具作为保留节目,那就是调换颜色版。从NES上的游戏《塞尔达传说》到Xbox的《光晕》,调色板的交换是一个非常简单的有效的视觉化设计,可以扩展艺术设计的极限。

 

 
 

在接下来的例子中,你可以学到如何使用层来交换调色板。我们使用一个运动的篮球队员来做例子。

 

如何去做…

 

第一件事,我们将绘制精灵并且再不同的区域上色:

 

1.在动态纹理上要上色的地方留下空白。在你的图片编辑项目中,你的问题看起来像这样:

 
 

2.创建一个新层并且为特殊的地方上白色。在这个例子中我们给他的衣服和裤子上色:

 

 

 

 
 

3.隐藏其他层,并且储存那个只有边框的层作为一个纹理。

 

4.为不同区域的有色层做一个纹理。

 

5.一旦我们有了这些纹理我们开始写代码:

 

@implementation Ch1_PaletteSwapping

-(CCLayer*) runRecipe

{

   //Create a nice looking background

   //创建一个看起来特别nice的背景

   CCSprite *bg = [CCSpritespriteWithFile:@"baseball_bg_02.png"];

   [bg setPosition:ccp(240,160)];

   bg.opacity = 100;

   [self addChild:bg z:0 tag:0];

   /*** Animate 4 different fielders with different color combinations ***/

   //4个不同的有不同颜色的运动员

   //Set color arrays

   //设置颜色

   ccColor3B colors1[] = {

       ccc3(255,217,161), ccc3(225,225,225), ccc3(0,0,150), ccc3(255,255,255)};

   ccColor3B colors2[] = {

       ccc3(140,100,46), ccc3(150,150,150), ccc3(255,0,0), ccc3(255,255,255) };

    ccColor3B colors3[] = {

       ccc3(255,217,161), ccc3(115,170,115), ccc3(115,170,115),ccc3(255,255,255) };

   ccColor3B colors4[] = {

       ccc3(140,100,46),  ccc3(50,50,50),ccc3(255,255,0), ccc3(255,255,255) };

   //Animate fielders with colors

   //上色

   [self animateFielderWithColors:colors1 withPosition:ccp(150,70)];

   [self animateFielderWithColors:colors2 withPosition:ccp(150,200)];

   [self animateFielderWithColors:colors3 withPosition:ccp(300,200)];

   [self animateFielderWithColors:colors4 withPosition:ccp(300,70)];

   return self;

}

-(void)animateFielderWithColors:(ccColor3B[])colors withPosition:(CGPoint)pos

{

   //The names of our layers

   //层的名字

   NSString *layers[] = { @"skin", @"uniform",@"trim", @"black_lines" };

   //Number of layers

   //层的数量

   int numLayers = 4;

   for(int i=0; i<numLayers; i+=1)

   {

       NSString *layerName = layers[i];

       ccColor3B color = colors[i];

       //We need each plist, the first frame name and finally a name for the animation

       //我们需要三个文件:plist文件,名字文件,动作名文件

       NSString *plistName = [NSStringstringWithFormat:@"fielder_run_%@.plist", layerName];

       NSString *firstFrameName = [NSStringstringWithFormat:@"fielder_run_%@_01.png", layerName];

       NSString *animationName = [NSStringstringWithFormat:@"fielder_run_%@", layerName];

       //Add plist frames to the SpriteFrameCache

       //加入pilst文件到SpriteFrameCache中

       [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:plistName];

        //Get the first sprite frame

       //获得第一个精灵帧

       CCSpriteFrame *firstFrame = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:firstFrameName];

       //Create our sprite

       //创建精灵

       CCSprite *sprite = [CCSprite spriteWithSpriteFrame:firstFrame];

       //Set color and position

       //设置颜色和位置

       sprite.position = pos;

       sprite.color = color;

       //Create the animation and add frames

       //创建动画

       CCAnimation *animation = [[CCAnimation alloc] initWithName:animationNamedelay:0.15f];

       for(int i=1; i<=8; i+=1)

       {

            CCSpriteFrame *frame =[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSStringstringWithFormat:@"fielder_run_%@_0%i.png",layerName,i]];

            [animation addFrame:frame];

       }

       //Run the repeating animation

       //运行动画

       [sprite runAction:[CCRepeatForever actionWithAction:[CCAnimateactionWithAnimation:animation]]];

       //Finally, add the sprite

       //最后加载这些精灵到场景

       [self addChild:sprite];

   }

}

@end

 

如何工作…

 

通过在主层(黑线框层)之下绘制颜色层的方式,我们掩盖了很多颜色层有锯齿的情况。这个技术对于不习惯用黑色边框来作图的美工有很一些难度。

 

效率问题——磁盘空间:

保持你的磁盘空间在一个可控的大小是很重要的事。这个技术对你的磁盘大小压力不大,由于png简单的纹理压缩,颜色层只占用了很小的一部分空间。

 

效率问题—内存信息:

不幸的是,这个材质贴图的尺寸对内存影响比较大。所以,如果你打算换一个很大的图片的颜色,你可能会有一些能源消耗的问题。交换纹理所造成的内存消耗等于几倍的正常内存的消耗。

 

效率问题——CPU

当我们进行颜色调换的时候,动作代码也会消耗更多的时间。不过,正如动画所占用的CPU的时间,这通常是无关紧要的。

 

(译)Cocos2d_for_iPhone_1_Game_Development_Cookbook

著作声明:本文由iam126 翻译,欢迎转载分享。

请尊重作者劳动,转载时保留该声 明和作者博客链接,谢谢!

相关程序代码下载:http://download.csdn.net/detail/iam126/4068610

或搜索“Cocos2d_for_iPhone_1_Game_Development_Cookbook代码”于CSDN;

新手翻译,不准确请见谅,参考代码与原书。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值