Fork me on GitHub

cocos2d 总结:三种 创建CCAnimation方法

http://o0o0o0o.iteye.com/blog/805581

1: CCSpriteBatchNode  

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 = 0; i < 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 sharedTextureCache] addImage:file];

// Assuming that image file animations always use the whole image for each animation frame.
CGSize texSize = texture.contentSize;
CGRect texRect = CGRectMake(0, 0, 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 = 0; i < 5; i++)
{
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 = 0; i < 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 sharedTextureCache] addImage:file];

// Assuming that image file animations always use the whole image for each animation frame.
CGSize texSize = texture.contentSize;
CGRect texRect = CGRectMake(0, 0, 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;
}


// Creates an animation from sprite frames.
+(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay
{
// load the ship's animation frames as textures and create a sprite frame
NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
for (int i = 0;



posted on 2012-04-06 17:59  pengyingh  阅读(2865)  评论(0)    收藏  举报

导航