cocos2d动画用例

来源:互联网 发布:五轴数控编程招聘 编辑:程序博客网 时间:2024/06/05 17:09

// Animation.h

#import"Box2D.h"

#import"cocos2d.h"

#import"BYSingle.h"

@interface AnimSpriteFactory :NSObject {

    BYSingle *_single;

    BOOL _is4g;

    BOOL _isIpad;

}


- (id) init;

- (CCSprite*) genAnimSprite:(CGPoint)position 

                   animName:(NSString*)animName 

                 startIndex:(int)startIndex 

                   endIndex:(int)endIndex 

              repeatForever:(BOOL)repeatForever 

                      delay:(float)delay;


- (void) dealloc;

@end




//  Animation.mm

#import"AnimSpriteFactory.h"

@implementation AnimSpriteFactory


- (id) init {

    if((self = [superinit])) {

        _single = [BYSingle getInstance];

_is4g =_single.isRetinaSupported;

_isIpad =_single.isIpad;

    }

   returnself;

}


/**

 * 用于获取 sprite 的宽度和高度,太他妈蛋疼了~

 *解析 plist 文件

 * 弊端:会使动画第一帧偏低于正常情况(当初不知道是出于什么意图要计算出动画的size)~

 */

-(CGSize) getAnimSpriteSize:(NSString*)animName start:(int)startIndex {

NSString *plistPath = [[NSBundlemainBundle]pathForResource:animNameofType:@"plist"];

NSDictionary *dictionary, *framesDic;

//dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

    dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];

framesDic = [dictionaryobjectForKey:@"frames"];

NSMutableDictionary *startFrameDic = [framesDicobjectForKey:[NSStringstringWithFormat:@"%@%d.png", animName, startIndex]];

NSString *rectDataString = [startFrameDicobjectForKey:@"frame"];

NSRange range =NSMakeRange(1, [rectDataStringlength]-2);

NSString *withoutBorder = [rectDataStringsubstringWithRange:range];

NSArray *array = [withoutBordercomponentsSeparatedByString:@", "];

NSString *sizeString = [arrayobjectAtIndex:1];

NSRange range2 =NSMakeRange(1, [sizeStringlength]-2);

NSString *sizeStringWithoutBorder = [sizeStringsubstringWithRange:range2];

NSArray *size = [sizeStringWithoutBordercomponentsSeparatedByString:@","];

returnCGSizeMake([[sizeobjectAtIndex:0]intValue], [[sizeobjectAtIndex:1]intValue]);

}


// delay默认值为 005~

- (CCSprite*) genAnimSprite:(CGPoint)position 

                   animName:(NSString*)animName 

                 startIndex:(int)startIndex 

                   endIndex:(int)endIndex 

              repeatForever:(BOOL)repeatForever 

                      delay:(float)delay 

{

   CCSpriteFrameCache *frameCache = [CCSpriteFrameCachesharedSpriteFrameCache];

    

   // 1.缓冲sprite帧和纹理

    NSString *spriteFrameCacheName = [NSStringstringWithFormat:@"%@.plist", animName];

    [frameCache addSpriteFramesWithFile:spriteFrameCacheName];

    

    /**

     * 2.创建一个精灵批处理结点

     * 该步操作放在这儿不好,水动画要用到很多个,这么做的话就会添加进很多重复的 batchNode ~

     * Eric 将这步放置在方法的外部,是个很好的做法!

     */

    NSString *spriteSheetName = [NSString stringWithFormat:@"%@.png", animName];

    CCSpriteBatchNode *spriteBatchNode = [CCSpriteBatchNodebatchNodeWithFile:spriteSheetName];

    [_single.gameLayeraddChild:spriteBatchNode];

    

   // 3.收集帧列表

   NSMutableArray *frames = [NSMutableArrayarray];

    for(int i = startIndex; i < endIndex+1; ++ i) {

        NSString *frameName = [NSString stringWithFormat:@"%@%d.png", animName, i];

        CCSpriteFrame *spriteFrame = [frameCache spriteFrameByName: frameName];

        [frames addObject: spriteFrame];

    }

    

   // 4.创建动画对象

    CCAnimation *animation = [CCAnimation animationWithFrames:frames delay:delay];

   id action = [CCAnimateactionWithAnimation:animationrestoreOriginalFrame:NO];

    id sequence = nil;

    if(repeatForever == YES) {

        id repeatAction = [CCRepeatForever actionWithAction:action];

        sequence = [CCSequenceactions:repeatAction,nil];

    } else {

        id callFunc = [CCCallFuncN actionWithTarget:self selector:@selector(removeSprite:)];

        sequence = [CCSequenceactions:action, callFunc,nil];

    }


   // 5.创建 sprite并且让它 run 动画 action~

    NSString *fristFrameName = [NSString stringWithFormat:@"%@%d.png", animName, startIndex];

    CCSprite *animSprite = [CCSprite spriteWithSpriteFrameName:fristFrameName];

//    CGSize size = [self getAnimSpriteSize:animName start:startIndex];

//    if(!_isIpad) {

//        [animSprite setContentSize:size];//只有这种方法才是有效的~

//    } else {

//        CGSize ipadSize = CGSizeMake(2*size.width, 2*size.height);

//        [animSprite setContentSize:ipadSize];

//    }

    [animSprite setPosition:position];

    

    /**

     *注意:

     * 1。先 runAction addChild

     * 2addChild方法要由 CCSpriteBatchNode对象调用,而不要由 _single.gameLayer 调用~

     *由后者调用的话就失去了使用 batchNode的意义~

     * (创建一个精灵批处理结点)

     */

    [animSprite runAction:sequence];

    [spriteBatchNodeaddChild:animSprite];

    return animSprite;

}


// Created by Eric~

-(CCSprite*) createAnimation:(NSString*)actionName 

                      Delay:(float)delay 

                 FrameCount:(int)num 

                  BanchNode:(CCSpriteBatchNode*)spriteSheet

{

   CCSpriteFrameCache *frameCache = [CCSpriteFrameCachesharedSpriteFrameCache];

[frameCacheaddSpriteFramesWithFile:[NSStringstringWithFormat:@"%@.plist",actionName]];

    

NSMutableArray *animFrames = [NSMutableArrayarray];

for(int i =0; i < num ; ++ i) {

        NSString *frameName = [NSString stringWithFormat:@"%@%d.png", actionName, i];

[animFramesaddObject:[frameCachespriteFrameByName:frameName]];

}

    

CCAnimation *anim = [CCAnimationanimationWithFrames:animFramesdelay:delay];

CCSprite *sprite = [CCSpritespriteWithSpriteFrameName:[NSStringstringWithFormat:@"%@1.png", actionName]]; 

[spriterunAction:[CCRepeatForeveractionWithAction:

  [CCAnimateactionWithAnimation:animrestoreOriginalFrame:NO]]];

[spriteSheetaddChild:sprite];

return sprite;

}


/** 待气球爆炸动作执行完毕之后,将 animSprite gameLayer中移除~ */

- (void) removeSprite:(id)sender {

    CCSprite *animSprite = (CCSprite*)sender;

    [_single.gameLayerremoveChild:animSpritecleanup:YES];

}


- (void) dealloc {

    [superdealloc];

}


@end



原创粉丝点击