2012-04-19 59 views
0

我开始玩Cocos2D,我想通过精灵表做一个精灵动画。Cocos2D角色动画

现在我有一个小机器人在我的屏幕上四处走动。但我想知道如何将一个动画放到另一个动画中。

例如,我的机器人有一个步行动画,我想单独移动他的手臂。但是他的肩膀在走路时会起伏,手臂应该相对于他的肩膀位置移动。

我的步行动画有五个精灵,我有九个不同的手臂位置。现在,我可以将武器添加到所有九个手臂位置的每一个步行精灵。但后来我会有45个图像,而不是14.

+0

你机器人需要成为spritesheet – 2012-04-19 18:27:59

回答

0

这是一个很好的问题(最后!)。

我会做的是将你的角色分成不同的精灵(你已经这样做了),这些精灵可以自己动画化。

然后,无论何时显示该动画的一帧,我都会执行一段代码来修改臂动画的位置,以便与肩膀相匹配。

要执行该代码块,您需要Cocos 1.1或更高版本,因为他们在那里添加了CCAnimationFrame,但是,这些框架只能通过NSNotification执行代码,所以我做了一个改进,因此我们可以设置一个块在一个框架上,并且该框架将在该框架被显示时执行。

只要找到CCAnimation.h和修改CCAnimationFrame界面看起来像这样:

typedef void(^FrameBlock)(CCSprite *sprite); 

/** CCAnimationFrame 
A frame of the animation. It contains information like: 
- sprite frame name 
- # of delay units. 
- offset 

@since v1.1 
*/ 
@interface CCAnimationFrame : NSObject <NSCopying> 
{ 
    CCSpriteFrame* spriteFrame_; 
    float delayUnits_; 
    NSDictionary *userInfo_; 

    FrameBlock frameBlock; 
} 
/** CCSpriteFrameName to be used */ 
@property (nonatomic, readwrite, retain) CCSpriteFrame* spriteFrame; 

/** how many units of time the frame takes */ 
@property (nonatomic, readwrite) float delayUnits; 

/** A CCAnimationFrameDisplayedNotification notification will be broadcasted when the frame is displayed with this dictionary as UserInfo. If UserInfo is nil, then no notification will be broadcasted. */ 
@property (nonatomic, readwrite, retain) NSDictionary *userInfo; 

/** If the block is not NULL, it will be executed when the frame becomes visible **/ 
@property (nonatomic, readwrite, copy) FrameBlock frameBlock; 

/** initializes the animation frame with a spriteframe, number of delay units and a notification user info */ 
-(id) initWithSpriteFrame:(CCSpriteFrame*)spriteFrame delayUnits:(float)delayUnits userInfo:(NSDictionary*)userInfo; 
-(id) initWithSpriteFrame:(CCSpriteFrame*)spriteFrame delayUnits:(float)delayUnits block:(FrameBlock)block; 
@end 

,然后打开CCAnimation.m并确保CCAnimationFrame实施看起来是这样的:

@implementation CCAnimationFrame 

@synthesize spriteFrame = spriteFrame_, delayUnits = delayUnits_, userInfo=userInfo_; 

@synthesize frameBlock; 

-(id) initWithSpriteFrame:(CCSpriteFrame *)spriteFrame delayUnits:(float)delayUnits userInfo:(NSDictionary*)userInfo 
{ 
    if((self=[super init])) { 
     self.spriteFrame = spriteFrame; 
     self.delayUnits = delayUnits; 
     self.userInfo = userInfo; 
    } 

    return self; 
} 

-(id) initWithSpriteFrame:(CCSpriteFrame*)spriteFrame delayUnits:(float)delayUnits block:(FrameBlock)block{ 
    self = [self initWithSpriteFrame:spriteFrame delayUnits:delayUnits userInfo:nil]; 
    if(self){ 
     [self setFrameBlock:block]; 
    } 
    return self; 
} 

-(void) dealloc 
{  
    CCLOGINFO(@"cocos2d: deallocing %@", self); 

    [spriteFrame_ release]; 
    [userInfo_ release]; 

    [super dealloc]; 
} 

-(id) copyWithZone: (NSZone*) zone 
{ 
    CCAnimationFrame *copy = [[[self class] allocWithZone: zone] initWithSpriteFrame:[[spriteFrame_ copy] autorelease] delayUnits:delayUnits_ userInfo:[[userInfo_ copy] autorelease] ]; 
    return copy; 
} 

-(NSString*) description 
{ 
    return [NSString stringWithFormat:@"<%@ = %08X | SpriteFrame = %08X, delayUnits = %0.2f >", [self class], self, spriteFrame_, delayUnits_ ]; 
} 
@end 

然后,当创建动画帧时,添加一个块,在其中设置手臂的位置以匹配肩膀。

我希望它有帮助。