2012-04-19 8 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 

, 애니메이션 프레임을 생성 할 때, 팔의 위치를 ​​설정하여 어깨와 일치시키는 블록을 추가하십시오.

도움이되기를 바랍니다.

관련 문제