2012-08-24 4 views
1

저는 Player 클래스를 사용하는 방식에 대한 비판과 개선 방법을 주로 생각합니다.Cocos2d - 터치를 기반으로 스프라이트 애니메이션 (스프라이트 시트) 변경

내 스프라이트 시트에는 내가 수행중인 작업에 필요한 모든 시퀀스가 ​​들어 있습니다. 그러나 처음에 내가 원하는 것은, "jump"버튼을 누르면 문자가 통과하는 애니메이션이 멈추고 사용 된 스프라이트 시트에서 "jump"시퀀스가 시작됩니다. (걷기 - 예를 들어 프레임 7에서 9까지 프레임 10에서 13까지 점프).

Player.m :

 player = [[Player alloc] playerCharacter]; 
     player.position = ccp(100,50); 
     [map addChild:player z:15]; 

내가 내 질문에 같아요 .. 내가 이동해야

-(id)playerCharacter { 
    if (self = [super init]) { 

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Soldier.plist"]; 

    // Create a sprite sheet with the Player images 
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"Soldier.png"]; 
    [self addChild:spriteSheet z:15]; 

    NSMutableArray *walking = [NSMutableArray array]; 
    for(int i = 7; i <= 9; ++i) { 
     [walking addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Soldier%d.png", i]]]; 
    } 

    CCAnimation *walkingAnimation = [CCAnimation animationWithSpriteFrames:walking delay:0.2f]; 

    _walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkingAnimation]]; 

    walkAnim.restoreOriginalFrame = NO; 

    [self runAction:_walkAction]; 

    self.velocity = ccp(0.0, 0.0); 
} 
return self; 
} 

GameLevelLayer.m 여기

는 내 플레이어 스프라이트를 생성하는 방법이다 해당 메서드의 전체 CCAnimation 구성 요소를 spriteheet/animation 시퀀스를 정의하는 일련의 BOOL을 검사하는 새로운 메서드로 변환합니까?

나는 위의 코드를 가장 효율적으로 사용하고 있습니까? 그것이 그것이 일하는 것을 인정하는 동안 나는 올바르게 일을하고 있는지, 아니면 내 프로젝트에 비효율적 인 것을 추가하는 길을 계속 수행하는지 이해할 것이다.

모든 의견과 도움에 매우 감사드립니다.

답변

1

CCSpriteBatchNode를 올바르게 사용하고 있지 않습니다. 스프라이트 시트 하나에서 모든 스프라이트를 보관할 레이어라고 생각하십시오. 플레이어가 batchnode에 추가되어야하고 batchnode가 게임 레이어에 추가되어야합니다. GameLevelLayer에서 다음

-(id)playerCharacterOnSheet:(CCSpriteBatchNode*)batchNode 
{ 
    // this is assuming Player is a CCSprite subclass 
    // set this to the "static"/default player sprite image 
    if (self = [super initWithSpriteFrameName:@"Soldier1.png"]) 
    { 
     // Load self to the batchnode 
     [batchNode addChild:self]; 

     NSMutableArray *walking = [NSMutableArray array]; 
     for(int i = 7; i <= 9; ++i) { 
      [walking addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Soldier%d.png", i]]]; 

     CCAnimation *walkingAnimation = [CCAnimation animationWithSpriteFrames:walking delay:0.2f]; 

     _walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkingAnimation]]; 

     walkAnim.restoreOriginalFrame = NO; 

     [self runAction:_walkAction]; 

     self.velocity = ccp(0.0, 0.0); 
    } 
    return self; 
} 

의 batchNode과 플레이어 작성 : 그래서 플레이어는 다음과 같이 작성해야

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

player = [[Player alloc] playerCharacterOnSheet:spriteSheet]; 
player.position = ccp(100,50); 

은 단순히을 수행 할 플레이어 클래스에 메소드를 추가, 다른 작업을 수행하려면을 (그리고 다른 실행중인 작업을 중지하십시오. 예를 들어 :

-(void)jumpAction 
{ 
    [self stopAllActions]; 

    NSMutableArray *jumping = [NSMutableArray array]; 
    for(int i = 10; i <= 13; ++i) { 
     [jumping addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Soldier%d.png", i]]]; 
     } 

    CCAnimation *jumpingAnimation = [CCAnimation animationWithSpriteFrames:jumping delay:0.2f]; 

    CCAction *jumpAction = [CCAnimate actionWithAnimation:jumpingAnimation ]; 
    [self runAction:jumpAction]; 

} 

수행하려면, 전화 :

[player jumpAction];