【问题标题】:Cocos2D Character animationCocos2D 角色动画
【发布时间】:2012-04-19 18:24:14
【问题描述】:

我开始玩 Cocos2D,我想用精灵表做一个精灵动画。

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

例如,我的机器人有一个行走动画,我想分别移动他的手臂。但是他的肩膀在行走过程中会上升和下降,手臂应该相对于他的肩膀位置移动。

我的行走动画有五个精灵,我有九个不同的手臂位置。现在,我可以为所有九个手臂位置的每个行走精灵添加手臂。但是我会有 45 张图片而不是 14 张。

【问题讨论】:

  • 你的机器人需要是一个精灵表

标签: iphone objective-c cocos2d-iphone


【解决方案1】:

这是一个很好的问题(终于!)。

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

然后,每当呈现该动画的一帧时,我都会执行一段代码来修改手臂动画的位置,使其与肩膀匹配。

要执行该代码块,您需要 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

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多