Animation Cocos2d一个精灵的两个动画
我的角色动画如下:Animation Cocos2d一个精灵的两个动画,animation,cocos2d-iphone,ccsprite,sprite-sheet,Animation,Cocos2d Iphone,Ccsprite,Sprite Sheet,我的角色动画如下: -(void) createHero { _batchNode = [CCSpriteBatchNode batchNodeWithFile:@"Snipe1.png"]; [self addChild:_batchNode]; [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Snipe1.plist"]; //gather list of fr
-(void) createHero
{
_batchNode = [CCSpriteBatchNode batchNodeWithFile:@"Snipe1.png"];
[self addChild:_batchNode];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Snipe1.plist"];
//gather list of frames
NSMutableArray *runAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 7; ++i)
{
[runAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"run000%d.png", i]]];
}
//create sprite and run the hero
self.hero = [CCSprite spriteWithSpriteFrameName:@"run0001.png"];
_hero.anchorPoint = CGPointZero;
_hero.position = self.heroRunningPosition;
//create the animation object
CCAnimation *runAnim = [CCAnimation animationWithFrames:runAnimFrames delay:1/30.0f];
self.runAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:runAnim restoreOriginalFrame:YES]];
[_hero runAction:_runAction];
[_batchNode addChild:_hero z:0];
}
但是现在我想要第二个plist和第二个png,所以当角色跳跃时,我会得到一个全新的动画。如何实现这一点?为跑步和跳跃创建两种不同的动画动作。根据需要运行这些动作。为跑步和跳跃创建两种不同的动画动作。根据需要运行这些操作。在我的例子中,我实现了一个AnimatedSprite类,它将为我处理这些操作。通过这种方式,我添加了如下文件:
NSDictionary* anims = [NSDictionary dictionaryWithObjectsAndKeys:
@"Animations/Character/idle_anim.plist", @"Idle",
@"Animations/Character/walk_anim.plist", @"Walk",
@"Animations/Character/run_anim.plist", @"Run", nil];
CCNode* myNode = [[AnimatedSprite alloc] initWithDictionary:anims
spriteFrameName: @"character_idle_01.png"
startingIndex:@"Idle"];
更改动画非常简单,如下所示:
[myNode setAnimation: @"Run"];
这是我的实现,这是
@interface AnimatedSprite : CCSprite
{
NSMutableDictionary* _spriteAnimations;
CCAction* _currentAnimation;
NSString* _currentAnimationName;
bool _initialized;
}
- (id) initWithTextureName:(NSString*) textureName;
- (id) initWithArray: (NSArray*) animList spriteFrameName: (NSString*) startingSprite startingIndex: (int)startingIndex;
- (id) initWithDictionary:(NSDictionary *)anims spriteFrameName:(NSString *)startingSprite startingIndex:(NSString *)startingAnim;
- (void) addAnimation: (NSString*) animationName andFilename: (NSString*) plistAnim;
- (void) setAnimationIndex: (int) index;
- (void) setAnimation: (NSString*) animationName;
@end
这是我的
#import "AKHelpers.h"
@implementation AnimatedSprite
NSMutableDictionary* _spriteAnimations;
- (id) initWithTextureName:(NSString*) textureName
{
CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:textureName];
CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect: CGRectMake(0, 0, 1, 1)];
if ((self=[super initWithSpriteFrame:frame]))
{
_currentAnimationName = nil;
_currentAnimation = nil;
_spriteAnimations = [[NSMutableDictionary alloc] init ];
_initialized = true;
}
return self;
}
- (id) initWithArray: (NSArray*) animList spriteFrameName: (NSString*) startingSprite startingIndex: (int)startingIndex
{
_initialized = false;
_spriteAnimations = [[NSMutableDictionary alloc] init];
// Add animations as numbers from 0 to animList.count
int i = 0;
for (NSString* anim in animList)
{
[self addAnimation: [NSString stringWithFormat:@"%d", i] andFilename:anim];
i++;
}
if ((self = [super initWithSpriteFrameName:startingSprite]))
{
_currentAnimationName = nil;
_currentAnimation = nil;
[self setAnimationIndex: startingIndex];
_initialized = true;
}
return self;
}
- (id) initWithDictionary:(NSDictionary *)anims spriteFrameName:(NSString *)startingSprite startingIndex:(NSString *)startingAnim
{
_initialized = false;
_spriteAnimations = [[NSMutableDictionary alloc] init];//[[NSMutableArray alloc] init];
// Add animations
for (NSString* key in anims)
{
[self addAnimation: key andFilename: [anims objectForKey: key] ];
}
if ((self = [super initWithSpriteFrameName:startingSprite]))
{
_currentAnimationName = nil;
_currentAnimation = nil;
[self setAnimation: startingAnim];
_initialized = true;
}
return self;
}
- (void) dealloc
{
[_currentAnimationName release];
[_spriteAnimations release];
[super dealloc];
}
- (void) setAnimation: (NSString*) animationName
{
if (![_currentAnimationName isEqualToString:animationName])
{
[_currentAnimationName release];
_currentAnimationName = [animationName copy];
// Stop current animation
if (_currentAnimation != nil)
[self stopAction:_currentAnimation];
//[self stopAllActions];
// Apply animation
NSDictionary* clip = [_spriteAnimations objectForKey: animationName];
if (clip)
{
_currentAnimation = [AKHelpers actionForAnimationClip:clip];
if (_currentAnimation)
[self runAction:_currentAnimation];
}
else
{
_currentAnimation = nil;
}
}
}
- (void) setAnimationIndex: (int) index
{
[self setAnimation: [NSString stringWithFormat:@"%d", index]];
}
- (void) addAnimation: (NSString*) animationName andFilename: (NSString*) plistAnim
{
NSDictionary *clip = [AKHelpers animationClipFromPlist:plistAnim];
if (clip)
{
[_spriteAnimations setObject:clip forKey:animationName];
if (_initialized && [_spriteAnimations count] == 1)
{
[self setAnimation:animationName];
}
}
}
@end
在我的例子中,我实现了一个AnimatedSprite类,它将为我处理这个问题。通过这种方式,我添加了如下文件:
NSDictionary* anims = [NSDictionary dictionaryWithObjectsAndKeys:
@"Animations/Character/idle_anim.plist", @"Idle",
@"Animations/Character/walk_anim.plist", @"Walk",
@"Animations/Character/run_anim.plist", @"Run", nil];
CCNode* myNode = [[AnimatedSprite alloc] initWithDictionary:anims
spriteFrameName: @"character_idle_01.png"
startingIndex:@"Idle"];
更改动画非常简单,如下所示:
[myNode setAnimation: @"Run"];
这是我的实现,这是
@interface AnimatedSprite : CCSprite
{
NSMutableDictionary* _spriteAnimations;
CCAction* _currentAnimation;
NSString* _currentAnimationName;
bool _initialized;
}
- (id) initWithTextureName:(NSString*) textureName;
- (id) initWithArray: (NSArray*) animList spriteFrameName: (NSString*) startingSprite startingIndex: (int)startingIndex;
- (id) initWithDictionary:(NSDictionary *)anims spriteFrameName:(NSString *)startingSprite startingIndex:(NSString *)startingAnim;
- (void) addAnimation: (NSString*) animationName andFilename: (NSString*) plistAnim;
- (void) setAnimationIndex: (int) index;
- (void) setAnimation: (NSString*) animationName;
@end
这是我的
#import "AKHelpers.h"
@implementation AnimatedSprite
NSMutableDictionary* _spriteAnimations;
- (id) initWithTextureName:(NSString*) textureName
{
CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:textureName];
CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect: CGRectMake(0, 0, 1, 1)];
if ((self=[super initWithSpriteFrame:frame]))
{
_currentAnimationName = nil;
_currentAnimation = nil;
_spriteAnimations = [[NSMutableDictionary alloc] init ];
_initialized = true;
}
return self;
}
- (id) initWithArray: (NSArray*) animList spriteFrameName: (NSString*) startingSprite startingIndex: (int)startingIndex
{
_initialized = false;
_spriteAnimations = [[NSMutableDictionary alloc] init];
// Add animations as numbers from 0 to animList.count
int i = 0;
for (NSString* anim in animList)
{
[self addAnimation: [NSString stringWithFormat:@"%d", i] andFilename:anim];
i++;
}
if ((self = [super initWithSpriteFrameName:startingSprite]))
{
_currentAnimationName = nil;
_currentAnimation = nil;
[self setAnimationIndex: startingIndex];
_initialized = true;
}
return self;
}
- (id) initWithDictionary:(NSDictionary *)anims spriteFrameName:(NSString *)startingSprite startingIndex:(NSString *)startingAnim
{
_initialized = false;
_spriteAnimations = [[NSMutableDictionary alloc] init];//[[NSMutableArray alloc] init];
// Add animations
for (NSString* key in anims)
{
[self addAnimation: key andFilename: [anims objectForKey: key] ];
}
if ((self = [super initWithSpriteFrameName:startingSprite]))
{
_currentAnimationName = nil;
_currentAnimation = nil;
[self setAnimation: startingAnim];
_initialized = true;
}
return self;
}
- (void) dealloc
{
[_currentAnimationName release];
[_spriteAnimations release];
[super dealloc];
}
- (void) setAnimation: (NSString*) animationName
{
if (![_currentAnimationName isEqualToString:animationName])
{
[_currentAnimationName release];
_currentAnimationName = [animationName copy];
// Stop current animation
if (_currentAnimation != nil)
[self stopAction:_currentAnimation];
//[self stopAllActions];
// Apply animation
NSDictionary* clip = [_spriteAnimations objectForKey: animationName];
if (clip)
{
_currentAnimation = [AKHelpers actionForAnimationClip:clip];
if (_currentAnimation)
[self runAction:_currentAnimation];
}
else
{
_currentAnimation = nil;
}
}
}
- (void) setAnimationIndex: (int) index
{
[self setAnimation: [NSString stringWithFormat:@"%d", index]];
}
- (void) addAnimation: (NSString*) animationName andFilename: (NSString*) plistAnim
{
NSDictionary *clip = [AKHelpers animationClipFromPlist:plistAnim];
if (clip)
{
[_spriteAnimations setObject:clip forKey:animationName];
if (_initialized && [_spriteAnimations count] == 1)
{
[self setAnimation:animationName];
}
}
}
@end
我在我的项目中添加了您的两个类,但我不知道如何实现它们我有Snipe1.plist Snipe2.plist和Snipe1.png和Snipe2.png。plist中有run0001-run0007.png,Snipe2中有jump0001-jump0007.png。现在如何添加这两个动画?谢谢你的回答!哦,什么是帮助者?我在哪里可以得到这个?你可以在这里找到AKHelpers,它有自己的plist动画格式。您所做的基本上是一个plist,它将引用纹理和坐标,然后定义帧和持续时间。您可以将plist设置为类似于此处的示例:实际上,我使用的是这样的内容:。这基本上让我每个动画只有一个plist,而不是在AnimationKitOk中使用整个行为系统。我现在将您的类添加到我的项目(AKHelper和AnimatedSprite)中。一切都很好。现在我将此plist添加到我的项目:并尝试此实现:。但我的应用程序崩溃了,为什么?怎么了?很抱歉链接,否则此评论将不会有字符。我在我的项目中添加了你的两个类,但我不了解它们的实现方式我有Snipe1.plist Snipe2.plist和Snipe1.png以及Snipe2.png。plist中有run0001-run0007.png,Snipe2中有jump0001-jump0007.png。现在如何添加这两个动画?谢谢你的回答!哦,什么是帮助者?我在哪里可以得到这个?你可以在这里找到AKHelpers,它有自己的plist动画格式。您所做的基本上是一个plist,它将引用纹理和坐标,然后定义帧和持续时间。您可以将plist设置为类似于此处的示例:实际上,我使用的是这样的内容:。这基本上让我每个动画只有一个plist,而不是在AnimationKitOk中使用整个行为系统。我现在将您的类添加到我的项目(AKHelper和AnimatedSprite)中。一切都很好。现在我将此plist添加到我的项目:并尝试此实现:。但我的应用程序崩溃了,为什么?怎么了?很抱歉链接,否则此评论将没有字符。