CCSprite 搜罗的创建动画的方法

1: CCSpriteBatchNode   ???????(可能是多个CCSprite)

 

CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"bullet.png"];

 

CCArray* bullets = [batch  children];

 

2:用多个单独的文件创建动画 CCAnimation, CCSpriteFrame

 

 

+(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay

{

    // load the animation frames as textures and create the sprite frames

    NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];

    for (int i 0i < frameCount; i++)

    {

       // Assuming all animation files are named "nameX.png" with X being a consecutive number starting with 0.

       NSString* file = [NSString stringWithFormat:@"%@%i.png", name, i];

       CCTexture2D* texture = [[CCTextureCache sharedTextureCacheaddImage:file];

 

       // Assuming that image file animations always use the whole image for each animation frame.

       CGSize texSize = texture.contentSize;

       CGRect texRect = CGRectMake(00, texSize.width, texSize.height);

       CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect offset:CGPointZero];

      

       [frames addObject:frame];

    }

   

    // create an animation object from all the sprite animation frames

    return [CCAnimation animationWithName:name delay:delay frames:frames];

}

 

//调用方式

CCAnimate* animate = [CCAnimate

actionWithAnimation:[self animationWithFile:@"ship-anim" //图片文件

frameCount:5 //帧数

delay:0.08f]//持续时间

];

 

3:*.plist 文件创建动画

 

1// Creates an animation from single files.

 

 

-(id) initWithShipImage

{

    // Load the Texture Atlas sprite frames, this also loads the Texture with the same name.

    CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];

    [frameCache addSpriteFramesWithFile:@"ship-and-bullet.plist"];

 

    // Loading the Ship's sprite using a sprite frame name (eg the filename)

    if ((self = [super initWithSpriteFrameName:@"ship.png"]))

    {

       // load the ship's animation frames as textures and create a sprite frame

       NSMutableArray* frames = [NSMutableArray arrayWithCapacity:5];

       for (int i 0i 5i++)

       {

           NSString* file = [NSString stringWithFormat:@"ship-anim%i.png"i];

           CCSpriteFrame* frame = [frameCache spriteFrameByName:file];

           [frames addObject:frame];

       }

      

       // create an animation object from all the sprite animation frames

       CCAnimation* anim = [CCAnimation animationWithName:@"move" delay:0.08f frames:frames];

      

       // add the animation to the sprite (optional)

       //[self addAnimation:anim];

      

       // run the animation by using the CCAnimate action

       CCAnimate* animate = [CCAnimate actionWithAnimation:anim];

       CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];

       [self runAction:repeat];

      

      

       [self scheduleUpdate];

    }

    return self;

}

 

// Creates an animation from single files.

+(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay

{

    // load the animation frames as textures and create the sprite frames

    NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];

    for (int i 0i < frameCount; i++)

    {

       // Assuming all animation files are named "nameX.png" with X being a consecutive number starting with 0.

       NSString* file = [NSString stringWithFormat:@"%@%i.png", name, i];

       CCTexture2D* texture = [[CCTextureCache sharedTextureCacheaddImage:file];

 

       // Assuming that image file animations always use the whole image for each animation frame.

       CGSize texSize = texture.contentSize;

       CGRect texRect = CGRectMake(00, texSize.width, texSize.height);

       CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect offset:CGPointZero];

      

       [frames addObject:frame];

    }

   

    // create an animation object from all the sprite animation frames

    return [CCAnimation animationWithName:name delay:delay frames:frames];

}

 

 

2) Creates an animation from sprite frames.

 

-(id) initWithShipImage

{

    // Load the Texture Atlas sprite frames, this also loads the Texture with the same name.

    CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];

    [frameCache addSpriteFramesWithFile:@"ship-and-bullet.plist"];

 

    // Loading the Ship's sprite using a sprite frame name (eg the filename)

    if ((self = [super initWithSpriteFrameName:@"ship.png"]))

    {

       // create an animation object from all the sprite animation frames

       CCAnimation* anim = [CCAnimation animationWithFrame:@"ship-anim" frameCount:5 delay:0.08f];

      

       // add the animation to the sprite (optional)

       //[self addAnimation:anim];

      

       // run the animation by using the CCAnimate action

       CCAnimate* animate = [CCAnimate actionWithAnimation:anim];

       CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];

       [self runAction:repeat];

      

      

       [self scheduleUpdate];

    }

    return self;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值