2012-04-19 9 views
0

私はCocos2Dで遊び始めました。スプライトシートを使ってスプライトアニメーションを行うことにしました。Cocos2Dキャラクターアニメーション

今、私は少しロボットが私の画面上を歩いている。しかし、私はアニメーションを別のアニメーションにどのように入れることができるのだろうかと思っています。

たとえば、私のロボットにはウォーキングアニメーションがあり、私は自分の腕を別々に動かしたいと思います。しかし、歩行中に肩が上がったり下がったりするため、腕は肩の位置に相対的に動くはずです。

私のウォークアニメーションには5つのスプライトがあり、私には9つの異なる腕の位置があります。今、私は9つの腕のすべてのポジションのために、すべての歩行スプライトに腕を追加することができました。しかし、私は14の代わりに45イメージを持っています。

+0

あなたはロボットがスプライトシートである必要があります –

答えて

0

これは良い質問です(ついに!)。

あなたのキャラクターは、自分でアニメーション化できる別のスプライト(すでにこれを行いました)に分かれています。

アニメーションのフレームが表示されるたびに、私は腕のアニメーションの位置を変更したコードブロックを実行して、肩にマッチします。

このコードブロックを実行するには、CCAnimationFrameをそこに追加したので、Cocos 1.1以上が必要ですが、これらのフレームは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 

その後アニメーションフレームを作成するときに、肩の位置に合わせてアームの位置を設定するブロックを追加します。

私はそれが役に立ちそうです。