iphone游戏开发之cocos2d (七) 自定义精灵类,实现精灵动画CCAnimation

holydancer原创,如需转载,请在显要位置注明:

转自holydancer的CSDN专栏,专栏地址:http://blog.csdn.net/holydancer


精灵是游戏的主角,我们在游戏中经常看到各种炫丽的精灵动画效果,之前我们提到精灵是由图片生成的,如果我们想要实现精灵的动画效果,比如捕鱼达人中摇尾游戏动的小鱼,就需要我们用很多张图片来生成一个个纹理,然后使纹理生成一个个的帧,再将这一个个的帧生成一个动画,额,说得有点乱,看代码会比较明白;

首先自定义一个精灵类fishSprite

fishSprite.h

[plain]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. #import "cocos2d.h"  
  3.   
  4. @interface fishSprite : CCSprite {  
  5.       
  6. }  
  7. +(id)fish;  
  8. @end  

fishSprite.m

[plain]  view plain copy
  1. @implementation fishSprite  
  2. +(id)fish  
  3. {  
  4.     return [[self alloc]initWithImage];  
  5. }  
  6.   
  7. -(id)initWithImage  
  8. {  
  9.     if ((self=[super initWithFile:@"fish1.png"])) {  
  10.         NSMutableArray* frames = [NSMutableArray arrayWithCapacity:18];  
  11.         //生成一个18的Array;  
  12.           
  13.         for (int i = 1; i < 19; i++)  
  14.         {  
  15.             NSString *pngFile = [NSString stringWithFormat:@"fish%d.png",i];  
  16.               
  17.             //利用已知图片名生成纹理;  
  18.             CCTexture2D *texture = [[CCTextureCache sharedTextureCache]addImage:pngFile];  
  19.               
  20.             //利用纹理生成组成动画的帧;  
  21.             CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:texture rect:CGRectMake(0, 0, texture.contentSize.width, texture.contentSize.height)];  
  22.               
  23.             //将生成的帧添加到数组中,共18个,之后我们要用这18个frame来构成动画;  
  24.             [frames addObject:frame];  
  25.               
  26.         }  
  27.           
  28.         //利用帧数组生成一个动画,设定帧与帧之间的切换频率为0.05;  
  29.         CCAnimation *animation =[CCAnimation animationWithSpriteFrames:frames delay:0.05];  
  30.           
  31.         //用CCAnimate将生成的CCAnimation转成可以用精灵操作的动作action:  
  32.         CCAnimate *animate = [CCAnimate actionWithAnimation:animation];  
  33.           
  34.         //设置为repeat  
  35.         CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate];  
  36.           
  37.         //执行  
  38.           
  39.          
  40.           
  41.           
  42.          
  43.         [self runAction:repeat];  
  44.         //这样,如果该精灵一被实例化成功,就会动起来;  
  45.   
  46.     }  
  47.     return self;  
  48. }  
  49. @end  

修改模板自动生成的IntroLayer

IntroLayer.h

[plain]  view plain copy
  1. #import "cocos2d.h"  
  2. #import "fishSprite.h"  
  3.   
  4. // HelloWorldLayer  
  5. @interface IntroLayer : CCLayer  
  6. {  
  7. }  
  8.   
  9. // returns a CCScene that contains the HelloWorldLayer as the only child  
  10. +(CCScene *) scene;  
  11.   
  12. @end  

IntroLayer.m

[plain]  view plain copy
  1. #import "IntroLayer.h"  
  2.   
  3.   
  4.   
  5. #pragma mark - IntroLayer  
  6.   
  7. // HelloWorldLayer implementation  
  8. @implementation IntroLayer  
  9.   
  10. // Helper class method that creates a Scene with the HelloWorldLayer as the only child.  
  11. +(CCScene *) scene  
  12. {  
  13.     // 'scene' is an autorelease object.  
  14.     CCScene *scene = [CCScene node];  
  15.       
  16.     // 'layer' is an autorelease object.  
  17.     IntroLayer *layer = [IntroLayer node];  
  18.       
  19.     // add layer as a child to scene  
  20.     [scene addChild: layer];  
  21.       
  22.     // return the scene  
  23.     return scene;  
  24. }  
  25.   
  26. //   
  27. -(void) onEnter//在载入该节点时被调用,有可能被调有多次;  
  28. {  
  29.     [super onEnter];  
  30.   
  31.     // ask director for the window size  
  32.     CGSize size = [[CCDirector sharedDirector] winSize];  
  33.   
  34.     CCSprite *background;  
  35.       
  36.       
  37.         background = [CCSprite spriteWithFile:@"bg.jpg"];  
  38.           
  39.         background.position = ccp(size.width/2, size.height/2);  
  40.   
  41.     // add the label as a child to this Layer  
  42.     [self addChild: background];  
  43.       
  44.     //在层上添加精灵;  
  45.     fishSprite *fish = [fishSprite fish];  
  46.     fish.position = ccp(160,120);  
  47.     [self addChild:fish];  
  48.   
  49.   
  50.       
  51. }  
  52.   
  53.   
  54. @end  

至于自动生成的helloWorld类直接删掉,这里不用它;另外,我们在工程里还需要添加组成帧动画的一个一个图片,如图所示;


这样运行后就可以看到一个不停运动的精灵,该精灵被创建后就是一个不断运动的效果,只需要指定位置,就会在指定的位置显示(图片不显示动画,懒得做gif了,亲们理解下啊),最后大家自己看下代码自己实现下,很简单的;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值