2011-08-08 3 views
0

누군가가 cocos2d에서 애니메이션을 다시 사용하는 방법을 설명 할 수 있는지 궁금합니다. 나는 일반적인 방법으로 소위하기 위해 사용되었다 : 나는, 부하 애니메이션에 '게임 객체'를 만들고 해당 단순히 gameObj->idle(); 또는 gameObj->walkToPoint();처럼 내 애니메이션을 호출 한 후 ...cocos2d의 애니메이션

내가 이러한 헬퍼 메소드 자신에 의해 정의되어야 함을 이해하고, CC2d에 대한 많은 가이드를 공부하고 애니메이션을 여러 가지 방식으로 사용했지만 가이드가 너무 단순해서 실제 사례를 촬영하지 않았습니다.

그래서 plist 및 png 텍스처에서 애니메이션을로드하고 부모 레이어에이 텍스처를 추가 한 다음 첫 번째 스프라이트를 'Game Object'스프라이트에 할당하는 메소드를 작성했습니다. 하지만 몇 가지 질문이 있습니다. 애니메이션을 재생할 수있는 방법을 찾을 수 없습니다.

상황이 좀 더 어렵습니다. (그러나 이것은 일반적인 경우입니다.) - 애니메이션을 '액션'에 할당해야하며 다른 유형의 작업이 있어야합니다. 예를 들어, 유휴 액션은 영원히 재생되고, 죽음은 한 번 재생되고 마지막 프레임에 머물러 액션을 한 번 쏘고 이전 프레임 (유휴 상태)을 복원합니다.

일을 간단하게하기 위해 복잡한 검사와 for..in 루프를 사용하여 기본 코드를 보여 드리겠습니다. (누가 cc2d 애니메이션 형식의 모든 프레임을 Array로 저장하고 사전으로 배열하지 않고 frame_%d으로로드할지 궁금합니다. 구조 Animations->Idle->FramesAnimations->Shoot->Frames 있지만, 주요되지 포인트 :))

//These things are global 
CCAnimation *idleAnim; 
CCAction * idleAction; // idle animation should be played forever 

CCAnimation *deathAnim; 
CCAction * deathAction; // death animation should be played once and stop at last frame 

CCAnimation *shootAnim; 
CCAction * shootAction; // shoot animation should be played once and idle frame restored 


    // loading animations with simple for loops 
    - (id)init { 
    self = [super initWithColor:ccc4(255,255,255,255)]; 
if (self) {  

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: 
    @"player.plist"]; 
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode 
             batchNodeWithFile:@"player.png"]; 

    [self addChild:spriteSheet]; 

    NSMutableArray *idleAnimFrames = [NSMutableArray array]; 
    for(int i = 1; i <= 20; ++i) { 
     [idleAnimFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
     [NSString stringWithFormat:@"hero_idle01_%04i_Layer-%i.png", i-1, i]]]; 
    } 

    idleAnim = [CCAnimation animationWithFrames:idleAnimFrames delay:0.1f]; 

    idleAction = [CCRepeatForever actionWithAction: 
        [CCAnimate actionWithAnimation:idleAnim restoreOriginalFrame:NO]]; 



    NSMutableArray *deathAnimFrames = [NSMutableArray array]; 
    for(int i = 0; i <= 30; ++i) { 
     [deathAnimFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
     [NSString stringWithFormat:@"hero_death01_%04i_Layer-%i.png", i, i+1]]]; 
    } 

    deathAnim = [CCAnimation animationWithFrames:deathAnimFrames delay:0.1f]; 

    deathAction = [CCAnimate actionWithAnimation:deathAnim restoreOriginalFrame:NO]; 





    NSMutableArray *shootAnimFrames = [NSMutableArray array]; 
    for(int i = 0; i <= 19; ++i) { 
     [shootAnimFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
     [NSString stringWithFormat:@"hero_idleshoot02_%04i_Layer-%i.png", i, i+1]]]; 
    } 

    shootAnim = [CCAnimation animationWithFrames:shootAnimFrames delay:0.1f]; 

    shootAction = [CCAnimate actionWithAnimation:shootAnim restoreOriginalFrame:YES]; 



    self.player = [CCSprite spriteWithSpriteFrameName:@"hero_idle01_0000_Layer-1.png"];   

    self.player.position = ccp(20, self.size.height/2); 

    [self.player runAction:shootAction]; 

    [spriteSheet addChild:self.player]; 

    [self.player runAction:[CCRotateTo actionWithDuration:0.0f angle:45]]; 



    [self schedule:@selector(gameLogic:) interval:1.0]; 
    } 
    return self; 
} 

이 코드가 제대로 (부하 유휴 애니메이션 실행하고 영원히 재생)하지만 난 행동과 잘 animatinos 사이를 전환 할 수있는 방법을 찾을 수 없습니다. 나는 예를 들어 누군가가 애니메이션을 촬영하지 않을 때 접근법을 보았습니다. 원래의 스프라이트가 촬영에서 첫 번째 스프라이트를 가져 와서 촬영을하고, 스프라이트를 삭제하고, 스프라이트를 원래대로 복원합니다. 하지만 스프라이트가 회전되면 어떻게 될까요? 하나는 플립, 회전, 스케일 등과 같은 원본 스프라이트의 모든 매개 변수를 sprite간에 앞뒤로 전달해야합니다. 보통 게임 객체는 CCSprite에서 상속되므로 일반적으로이 메소드는 주 게임 객체를 항상 삭제하고 복원해야한다는 의미입니다. 이것은 나를 위해 명확하지 않습니다.

나는 다음 일을 시도했지만 그들은 나를 위해 작동하지 않았다. 프로그램이 멈추었고 아무 것도 디버깅을 위해 작성되지 않았다. 나는 오류가 다른 스레드에서 발생하기 때문에 터치 처리를 담당한다.

// somewhere after touch happened in [shootToPoint:(CGPoing)point] method 
    [idleAction stop]; 
    [shootAction startWithTarget:self.player]; 
    [self.player runAction:[CCSequence actions: 
          shootAction, 
          [CCCallFuncN actionWithTarget:self selector:@selector(shooted)], 
          nil]]; 

그리고 shooted 방법 I는 다음과 같이 다시 전화 :

[shootActino stop]; // or [self.player stopAllActions] - doesn't matter I guess 
    [idleAction startWithTarget:self.player]; 
    CCSprite *sprite = (Bullet *)sender; // remove bullet sprite from memory 
    [self removeChild:sprite cleanup:YES]; // remove bullet sprite from memory 
    // other routines... 

그래서 사람이 어떻게 애니메이션을 전환하는 도우미 메서드와 게임 객체를 생성하는 나를 설명 할 수 있습니까? cc2d 문서 또는 웹의 다른 튜토리얼에서이 간단한 문제를 해결하는 방법이 불분명하기 때문에 나를 cc2d 문서로 보내지 마십시오.

답변

2

alloc/initWith 대신 도우미 메소드를 사용하여 인스턴스를 생성하기 때문에 자동 가져 오기가 수행되므로 충돌이 발생합니다.

은 예를 들어 당신의 유휴 애니메이션은 다음과 같이 작성해야합니다 :

NSMutableArray *idleAnimFrames = [[NSMutableArray alloc] init]; 
for(int i = 1; i <= 20; ++i) { 
    [idleAnimFrames addObject: 
    [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
     [NSString stringWithFormat:@"hero_idle01_%04i_Layer-%i.png", i-1, i]]]; 
} 

CCAnimation *idleAnimation = [[CCAnimation alloc] initWithFrames:idleAnimFrames delay:0.1f]; 
[idleAnimFrames release]; 

CCAnimate *idleAnimate = [[CCAnimate alloc] initWithAnimation:idleAnimation restoreOriginalFrame:NO]; 
[idleAnimation release]; 

idleAction = [[CCRepeatForever alloc] initWithAction:idleAnimate]; 
[idleAnimate release]; 

당신은 당신이 다른 곳에서 할 때 당신의 dealloc 방법에 할, 또는해야하는 (을 해제 할 때까지 여러 번이 작업을 사용할 수있는 방법을 그것으로 끝난다.).

+0

아, 잠시 동안 objective-c로 작업했지만 여전히 이러한 유지/해제 함정에 빠져 있습니다.나는 정확한 답이 어딘가에 있다는 것을 알았다. :) 대단히 감사합니다! – asdf

관련 문제