본문 바로가기

프로그래밍/cocos2d

CCAnimate 애니메이션 구현하기

스프라이트 애니메이션을 적용해 캐릭터가 걸어 다니게 만듭니다.


//

//  GameLayer.h

//  GameDemo

//

//  Created by cmpak on 5/10/10.

//  Copyright 2010 thefirstgood.com. All rights reserved.

//


#import "cocos2d.h"


@interface GameLayer : CCLayer {

    CGSize winSize;

    

    // 방향 전환에 쓰일 버튼

    // 눌리기 전과 눌렸을 때에 있도록 방향별로 두개씩 만든다.

    CCSprite  *rightSprite;

    CCSprite  *rightPressedSprite;

    CCSprite  *leftSprite;

    CCSprite  *leftPressedSprite;

    

    BOOL isLeftPressed;

    BOOL isRightPressed;

    

    // 주인공 캐릭터 - 여자 이미지를 사용하지만 prince라고 부르겠습니다.

    CCSprite *princeSprite;

    

    // 주인공 캐릭터의 걷기 애니메이션

    CCAnimate *princeWalkAnimate;

}


@property (nonatomic, retain) CCSprite  *rightSprite;

@property (nonatomic, retain) CCSprite  *rightPressedSprite;

@property (nonatomic, retain) CCSprite  *leftSprite;

@property (nonatomic, retain) CCSprite  *leftPressedSprite;

@property (nonatomic, retain) CCSprite  *princeSprite;

@property (nonatomic, retain) CCAnimate *princeWalkAnimate;


- (void) createBackgroundParallax;

- (void) createArrowButtons;

//- (void) createPrinceSprite;

- (void) createPrinceAndAnimation;


- (void) moveBackground;


- (void) startPrinceWalking;

- (void) stopPrinceWalking;


@end






//

//  GameLayer.m

//  GameDemo

//

//  Created by cmpak on 5/10/10.

//  Copyright 2010 thefirstgood.com. All rights reserved.

//


#import "GameLayer.h"


#define IMG_WIDTH 1600


enum {

    kTag_Parallax,

    kTag_Prince,

    kTag_PrinceSpriteSheet,

    kTag_ArrowButtonPressed,

    kTag_ArrowButton

};



@implementation GameLayer


@synthesize rightSprite, rightPressedSprite, leftSprite, leftPressedSprite;

@synthesize princeSprite, princeWalkAnimate;


- (id) init {

    if( (self=[super init]) ) {

        // CCLayer 터치이벤트를 처리할 있도록 활성화시킵니다.

        self.isTouchEnabled = YES;

        

        // 화면의 픽셀 크기를 구합니다.

        winSize = [[CCDirector sharedDirector] winSize];

        

        [self createBackgroundParallax];

        //[self createPrinceSprite];

        [self createPrinceAndAnimation];

        [self createArrowButtons];

    }

    

    return self;

}


- (void) onExit {

    [super onExit];

    

    // sprite sheet texture 캐시를 모두 지웁니다.   

    // 이상 사용하지않는 캐시를 반드시 지워주세요.

    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];

}


/*- (void) createPrinceSprite {

    // 이미지를 이용하여 주인공 스프라이트를 만듭니다.

    CCSprite *sprite = [[CCSprite alloc] initWithFile:@"prince.png"];

    self.princeSprite = sprite;

    [self addChild:self.princeSprite z:kTag_Prince tag:kTag_Prince];

    

    // 위치계산이 편하도록 주인공의 anchorPoint 가운데 아래로 잡습니다.

    self.princeSprite.anchorPoint = ccp(0.5, 0);

    

    self.princeSprite.position = ccp(winSize.width / 2, 13);

    

    [sprite release];

}*/


- (void) createPrinceAndAnimation {

    // prince_walk_notrim.png 파일로 CCSpriteSheet 만든 layer 넣습니다.

    //CCSpriteSheet* spriteSheet = [CCSpriteSheet spriteSheetWithFile:@"prince_walk_notrim.png"];

    //[self addChild:spriteSheet z:kTag_PrinceSpriteSheet tag:kTag_PrinceSpriteSheet];

    

    // 위치정보 파일을 읽어들여 바로 CCSpriteFrame 만들어 캐시에 저장합니다.

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

    

    // 프레임을 담을 Array 만듭니다.

    NSMutableArray *aniFrames = [NSMutableArray array];

    

    /*

    // sprite sheet 안의 각각의 프레임 크기는 88 * 146 픽셀입니다.

    CGSize frameSize = CGSizeMake(88, 146);

    

    // 모두 15개의 이미지로 프레임을 만든 array 담습니다.  

    // 첫번째 이미지의 rect (0, 0, 88, 146), 두번째는 (88, 0, 88, 146), 

    // 세번째는 (176, 0, 88, 146), ...

    // 그러므로 n번째 이미지의 x 좌표는 88 * (n-1) 됩니다.

    for(NSInteger idx = 0; idx < 15; idx++) {

        // 첫째 줄에 11개의 프레임이 있고, 12번째 부터는 번째 줄에 있으므로,

        // 12번째(idx==11)부터는 y 좌표의 값을 증가시켜야 합니다.

        NSUInteger index = idx % 11;

        NSUInteger rowIndex = idx / 11;

        

        CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:spriteSheet.texture

                                    rect:CGRectMake(frameSize.width * index, frameSize.height * rowIndex, 

                                                frameSize.width, frameSize.height)

                                    offset:CGPointZero];       

        [aniFrames addObject:frame];

    }*/

    

    // 아주 간단히 프레임을 만들어 배열에 저장할 있습니다. 스프라이트 시트를 만들 사용된

    // 개별 이미지의 파일 이름을 사용하여 개별 프레임을 읽어들입니다.

    for(NSInteger idx = 1; idx <= 15; idx++) {

        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache

                                spriteFrameByName:[NSString stringWithFormat:@"prince_walk_%02d.png", idx]];

        [aniFrames addObject:frame];

    }

    

    // 프레임으로 CCAnimation 만듭니다 프레임당 시간을 0.05초로 정해줍니다.

    CCAnimation *animation = [CCAnimation animationWithName:@"prince_walk" delay:0.05f frames:aniFrames];

    

    // CCAnimation action CCAnimate 만듭니다.

    CCAnimate *animate = [[CCAnimate alloc] initWithAnimation:animation restoreOriginalFrame:NO];

    self.princeWalkAnimate = animate;

    [animate release];

    

    // 제일 첫번째 프레임으로 주인공 sprite 만듭니다.

    //CCSprite *sprite = [CCSprite spriteWithSpriteFrame:(CCSpriteFrame*)[aniFrames objectAtIndex:0]];

    

    // 첫번째 프레임을 만들 사용했던 이미지 파일 이름을 사용하여 주인공 sprite 만듭니다.

    CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"prince_walk_01.png"];

    self.princeSprite = sprite;

    [self addChild:self.princeSprite z:kTag_Prince tag:kTag_Prince];

    

    // 위치계산이 편하도록 주인공의 anchorPoint 가운데 아래로 잡습니다.

    self.princeSprite.anchorPoint = ccp(0.5, 0);

    

    // 주인공을 화면 가운데 아래에서 13픽셀 위에 위치시킵니다.

    self.princeSprite.position = ccp(winSize.width / 2, 13);

    

    [sprite release];

}


- (void) createArrowButtons {

    // 왼쪽 화살표

    CCSprite *sprite = [[CCSprite alloc] initWithFile:@"arrow_left.png"];

    self.leftSprite = sprite;

    

    // 기본 anchorPoint 가운데, (0.5, 0.5)이므로 이미지의 가로 세로 크기의 반에 5픽셀의 

    // 여유를 두고 화면 아래에 표시합니다.

    self.leftSprite.position = ccp(10 + self.leftSprite.contentSize.width / 2

                                   self.leftSprite.contentSize.height / 2 + 5);

    

    [self addChild:self.leftSprite z:kTag_ArrowButton];

    [sprite release];

    

    // 눌렸을 쓰일 왼쪽 화살표 

    sprite = [[CCSprite alloc] initWithFile:@"arrow_left_s.png"];

    self.leftPressedSprite = sprite;

    

    // self.leftSprite 똑같은 위치에 표시합니다.

    self.leftPressedSprite.position = self.leftSprite.position;

    

    // 눌렸을 때의 화살표를 하위 z-order 넣습니다. 그럼, 위에 같은 크기의 화살표가 

    // 똑같은 위치에 있으니까 가려서 화면상에 보이지 않게됩니다.

    [self addChild:self.leftPressedSprite z:kTag_ArrowButtonPressed];

    [sprite release];

    

    // 오른쪽 화살표

    sprite = [[CCSprite alloc] initWithFile:@"arrow_right.png"];

    self.rightSprite = sprite;

    

    // 왼쪽 화살표에서 15픽셀 오르쪽에 위치시킵니다.

    self.rightSprite.position = ccp(self.leftSprite.position.x + self.rightSprite.contentSize.width + 15

                                    self.leftSprite.position.y);

    [self addChild:self.rightSprite z:kTag_ArrowButton];

    [sprite release];

    

    // 눌렸을 쓰일 오른쪽 화살표 

    sprite = [[CCSprite alloc] initWithFile:@"arrow_right_s.png"];

    self.rightPressedSprite = sprite;

    

    // self.rightSprite 똑같은 위치에 표시합니다.

    self.rightPressedSprite.position = self.rightSprite.position;

    

    // 눌렸을 때의 화살표를 하위 z-order 넣습니다. 그럼, 위에 같은 크기의 화살표가 

    // 똑같은 위치에 있으니까 가려서 화면상에 보이지 않게됩니다.

    [self addChild:self.rightPressedSprite z:kTag_ArrowButtonPressed];

    [sprite release];

}


- (void) createBackgroundParallax {

    // 이미지로 백그라운드에 쓰일 CCSprite 만듭니다.

    CCSprite *bgSprite1 = [CCSprite spriteWithFile:@"background1.png"];

    CCSprite *bgSprite2 = [CCSprite spriteWithFile:@"background2.png"];

    

    // Transform 사용되는 anchorPoint 왼쪽 아래 귀퉁이 (0, 0) 잡습니다

    bgSprite1.anchorPoint = ccp(0, 0);

    bgSprite2.anchorPoint = ccp(0, 0);

    

    // 위에서 만든 sprite 담을 parent CCParallaxNode 만듭니다.

    CCParallaxNode *voidNode = [CCParallaxNode node];

    

    // 배경 sprite Parallax 넣습니다.

    // parallaxRatio 가로/세로로 움직이는 속도라고 보시면 되겠습니다

    // 우리는 가로로만 움직이므로 y 값을 0으로 줍니다.

    

    // background1.png 파일의 세로 크기가 160 픽셀이기 때문에 positionOffset 이용하여 

    // 화면 위에 위치하도록 좌표를 조정합니다.

    // 뒤쪽에 깔릴 배경인 background1.png 천천히 움직이도록 parallaxRatio x 값을 1보다 작은

    // 0.4 설정합니다.

    [voidNode addChild:bgSprite1 z:0 parallaxRatio:ccp(0.4f, 0) positionOffset:ccp(0, winSize.height / 2)];    

    [voidNode addChild:bgSprite2 z:1 parallaxRatio:ccp(1.0f, 0) positionOffset:CGPointZero];


    [self addChild:voidNode z:kTag_Parallax tag:kTag_Parallax];

}



#pragma mark -

#pragma mark Game Play


- (void) startMovingBackground {

    // 만약 버튼 두개가 눌려졌으면 화면을 이동시키지 않습니다.

    if(isLeftPressed == YES && isRightPressed == YES)

        return;

    

    NSLog(@"start moving");

    [self schedule:@selector(moveBackground)];

    

    // 걷기 애니메이션을 시작합니다.

    [self startPrinceWalking];

}


- (void) stopMovingBackground {

    NSLog(@"stop moving");

    [self unschedule:@selector(moveBackground)];

    

    // 걷기 애니메이션을 멈춥니다.

    [self stopPrinceWalking];

}



- (void) moveBackground {

    // GameLayer 들어있는 parallax node 받습니다.

    CCNode *voidNode = [self getChildByTag:kTag_Parallax];

    

    // 프레임마다 움직일 거리

    CGPoint moveStep = ccp(3, 0);

    

    // 오른쪽 버튼이 눌려졌을 때는 반대로 움직임

    if(isRightPressed)

        moveStep.x = -moveStep.x;

    

    CGFloat bgParallaxRatio = 1.0f;

    

    CGPoint newPos = ccp(voidNode.position.x + moveStep.x, voidNode.position.y);

    

    // 배경이 양쪽 끝에 도달하면 이상 움직이지 않음

    if(isLeftPressed == YES && newPos.x > 0)

        newPos.x = 0;

    else if(isRightPressed == YES && newPos.x < -(IMG_WIDTH - winSize.width) / bgParallaxRatio)

        newPos.x = -(IMG_WIDTH - winSize.width) / bgParallaxRatio;

    

    // 주인공이 화면 가운데 있을 경우에만 배경을 움직입니다.

    CGFloat halfWinWidth = winSize.width / 2;

    if(self.princeSprite.position.x == halfWinWidth)

        voidNode.position = newPos;

    

    

    // 주인공의 방향을 정합니다.

    // flipX 이용하여 하나의 이미지로 방향을 표현할 있습니다.

    if(isRightPressed == YES)

        self.princeSprite.flipX = NO;

    else

        self.princeSprite.flipX = YES;

    

    // 만약 주인공이 화면 가운데 있지않을 경우에는 주인공을 화면가운데까지

    // 이동하게 합니다.

    

    if(isRightPressed == YES && self.princeSprite.position.x < halfWinWidth) {

        // moveStep.x 부호를 바꾼 이유는 배경과 주인공의 움직임 방향이 서로 반대이기 때문입니다.

        self.princeSprite.position = ccp(self.princeSprite.position.x + (moveStep.x * -1),

                                         self.princeSprite.position.y);

        // 가운데 이상 움직이지 않도록 체크합니다.

        if(self.princeSprite.position.x > halfWinWidth)

            self.princeSprite.position = ccp(halfWinWidth, self.princeSprite.position.y);

    }else if(isLeftPressed == YES && self.princeSprite.position.x > halfWinWidth) {

        // moveStep.x 부호를 바꾼 이유는 배경과 주인공의 움직임 방향이 서로 반대이기 때문입니다.

        self.princeSprite.position = ccp(self.princeSprite.position.x + (moveStep.x * -1),

                                         self.princeSprite.position.y);

        

        // 가운데 이상 움직이지 않도록 체크합니다.

        if(self.princeSprite.position.x < halfWinWidth)

            self.princeSprite.position = ccp(halfWinWidth, self.princeSprite.position.y);

    }

    

    // 배경의 끝에 도달하면 배경은 움직이지 않고 주인공을 화면 끝까지 이동시킵니다.

    if(newPos.x == 0 || newPos.x == -(IMG_WIDTH - winSize.width)) {

        CGPoint newPrincePos = ccp(self.princeSprite.position.x + (moveStep.x * -1), self.princeSprite.position.y);

        

        // 주인공이 화면의 왼쪽 또는 오른쪽 끝까지 도달했을 때는 이상 움직이지 않습니다.

        CGFloat halfWidth = self.princeSprite.contentSize.width / 2;

        if(newPrincePos.x <= halfWidth)

            newPrincePos.x = halfWidth;

        else if(newPrincePos.x >= winSize.width - halfWidth)

            newPrincePos.x = winSize.width - halfWidth;

        

        self.princeSprite.position = newPrincePos;

    }

}


- (void) startPrinceWalking {

    NSLog(@"주인공 걷기 시작");

    [self.princeSprite runAction:[CCRepeatForever actionWithAction:self.princeWalkAnimate]];

}


- (void) stopPrinceWalking {

    NSLog(@"주인공 걷기 ");

    [self.princeSprite stopAllActions];

}


#pragma mark -

#pragma mark Touch Event Handling


// 터치가 버튼 Sprite안에서 이루어졌는지 확인합니다.

- (BOOL) isTouchInside:(CCSprite*)sprite withTouch:(UITouch*)touch {

    // Cocoa 좌표 

CGPoint location = [touch locationInView: [touch view]];

    

    // Cocoa 좌표를 cocos2d 좌표로 변환합니다

CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];

    

    CGFloat halfWidth = sprite.contentSize.height / 2.0;

    CGFloat halfHeight = sprite.contentSize.height / 2.0;

    

    if(convertedLocation.x > (sprite.position.x + halfWidth) ||

       convertedLocation.x < (sprite.position.x - halfWidth) ||

       convertedLocation.y < (sprite.position.y - halfHeight) ||

       convertedLocation.y > (sprite.position.y + halfHeight) ) {

        return NO;

    }

    

    return YES;

}


// 손가락이 닫는 순간 호출됩니다.

- (void) ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

    UITouch *touch = [touches anyObject];

    

    // 아래 Boolean 변수 대신에 leftSprite rightSprite visible 값을 직접 사용해도 무방합니다.

    isLeftPressed = NO;

    isRightPressed = NO;

    

    // 터치가 왼쪽 또는 오른쪽 화살표 안에 들어왔는지 확인합니다.

    if([self isTouchInside:self.leftSprite withTouch:touch] == YES) {

        // 왼쪽 화살표를 안보이게 합니다. 그럼 아래에 있던 눌릴 보여지는 이미지가 나타날 것입니다.

        self.leftSprite.visible = NO;

        

        isLeftPressed = YES;

    }else if([self isTouchInside:self.rightSprite withTouch:touch] == YES) {

        // 오른쪽 화살표를 안보이게 합니다.

        self.rightSprite.visible = NO;

        

        isRightPressed = YES;

    }

    

    // 버튼이 눌려졌으면 화면을 움직입니다.

    if(isLeftPressed == YES || isRightPressed == YES)

        [self startMovingBackground];

}


// 손가락을 떼는 순간 호출됩니다.

- (void)ccTouchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {

    // 배경화면을 멈춥니다.

    if(isLeftPressed == YES || isRightPressed == YES)

        [self stopMovingBackground];

    

    // 감춰졌던 버튼이미지를 다시 보이게 합니다.

    if(isLeftPressed == YES)

        self.leftSprite.visible = YES;

    

    if(isRightPressed == YES

        self.rightSprite.visible = YES;

}


// 손가락을 움직일 계속해서 호출됩니다.

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    

    // 손가락이 버튼을 벗어나면 움직임을 중단합니다.

    if(isLeftPressed == YES && [self isTouchInside:self.leftSprite withTouch:touch] == NO) {

        self.leftSprite.visible = YES;

        [self stopMovingBackground];

    }else if(isRightPressed == YES && [self isTouchInside:self.rightSprite withTouch:touch] == NO) {

        self.rightSprite.visible = YES;

        [self stopMovingBackground];

    }

}



//- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {}


#pragma mark -

#pragma mark Memory Release


- (void) dealloc {

    [rightSprite release];

    [rightPressedSprite release];

    [leftSprite release];

    [leftPressedSprite release];

    

    [princeSprite release];

    [princeWalkAnimate release];

    

    [super dealloc];

}


@end


(1) schedule 메서드는 타이머를 사용하여 일정한 시간 간격으로 @selector에 정의된 메서드를 계속하여 호출합니다. 아무 파라미터를 사용하지 않을 경우에는 매 프레임 간격마다 호출됩니다. 만일 시간 간격을 정하고 싶을 때는 아래 메서드를 사용합니다. 
ccTime은 float와 같은 타입입니다.

(void)schedule:(SEL)selector interval:(ccTime)interval;

(2) unschedule 메서드를 사용하면 스케줄러에 등록되어 있던 스케줄이 지워집니다.