我开始玩Cocos2D,我想出了一个用精灵表做一个精灵动画。
现在我的屏幕上有一个小机器人在走来走去。但是我想知道如何将一个动画放入另一个动画中。
例如,我的机器人有一个行走动画,我想单独移动他的手臂。但是他的肩膀在走路时会起伏,手臂应该相对于他的肩膀位置移动。
我的行走动画有五个精灵,我有九个不同的手臂位置。现在,我可以将手臂添加到所有九个手臂位置的每个行走精灵中。但那时我会有 45 张图像而不是 14 张。
这是一个很好的问题(终于!
我要做的是将你的角色分成不同的精灵(你已经这样做了),这些精灵本身是可动画的。
然后,每当呈现该动画的一帧时,我都会执行一个代码块,该代码块修改了手臂动画的位置,使其与肩膀相匹配。
要执行该代码块,您需要 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
然后,在创建动画帧时,向其添加一个块,您可以在其中设置手臂的位置以匹配肩膀。
我希望它有所帮助。