cocos2d 总结:三种 创建CCAnimation方法

本文介绍如何使用Cocos2d-x通过多种方式创建动画,包括直接使用多个单独的图片文件、通过*.plist文件加载纹理集以及创建精灵帧缓存等方法。详细展示了动画的创建过程,并提供了具体代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;



转载于:https://www.cnblogs.com/pengyingh/articles/2435054.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值