现在的位置: 首页 > 综合 > 正文

cocos2d 创建动画

2013年09月09日 ⁄ 综合 ⁄ 共 941字 ⁄ 字号 评论关闭

1、创建方法

//创建动画动作
-(CCAction *)createAnimation:(NSString *)fishFileName delay:(float)delay typeFish:(int)typeFish PicNum:(int)PicNum{
    
    NSLog(@" delay: %f,typeFish:%d,PicNum:%d",delay,typeFish,PicNum);
    
    NSMutableArray *frames=[NSMutableArray array];
    for(int i=1;i<=PicNum;i++)
    {
        NSString *fileName=[NSString stringWithFormat:@"fish%d_%d_0.png",typeFish,i];
        NSLog(@"     %@",fileName);
        CCSprite *sFish=[CCSprite spriteWithFile:fileName];
        if(sFish==nil){
            break;
        }
        CCSpriteFrame *frame=[CCSpriteFrame frameWithTexture:sFish.texture rect:sFish.textureRect];
        [frames addObject:frame];
    }
    CCAnimation *anima=[CCAnimation animationWithFrames:frames delay:delay]; 
    id act=nil;
    act=[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:anima restoreOriginalFrame:NO]];
    return act;
}

2、调用

  

    id ac=[self createAnimation:nil delay:0.3  typeFish:ranTypeFish  PicNum:6];
    CCSprite *sprite=[CCSprite spriteWithFile:[NSString stringWithFormat:@"fish%d_1_0.png",ranTypeFish]];
    [self addChild:sprite];
    [sprite runAction:ac];

抱歉!评论已关闭.