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了,亲们理解下啊),最后大家自己看下代码自己实现下,很简单的;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值