[Animations] cocos2d-x 에니메이션 적용하기

|

Animations

Frame Animation

아래와 같이 여러개의 이미지 파일로 애니메이션을 만들 수 있다.

 1CCAnimation *animation = CCAnimation::create();
 2
 3// load image file from local file system to CCSpriteFrame, then add into CCAnimation
 4for (int i = 1; i < 15; i++)
 5{
 6    char szImageFileName[128] = {0};
 7    sprintf(szImageFileName, "Images/grossini_dance_%02d.png", i);
 8    animation->addSpriteFrameWithFileName(szImageFileName);  
 9}
10
11animation->setDelayPerUnit(2.8f / 14.0f); // 14개의 이미지를 2.8초 동안 보여줄때 각 프레임 간격.
12animation->setRestoreOriginalFrame(true); // 14개의 이미지를 돌린다음에 다시 1프레임으로 돌아간다
13
14CCAnimate *action = CCAnimate::create(animation);
15sprite->runAction(action);  // run action on sprite object

Note that CCAnimation is composed by sprite frames, delay time per frame, durations etc, it's a pack of "data". While CCAnimate is an action, it is created base on CCAnimation object.


Sprite Sheet Animation

위 에니메이션 방식은 이해하기 쉽지만, 실제 게임에서는 잘 사용되지 않습니다, 대신 스프라이트 시트 에니메이션을 일반적으로 사용합니다.

다음은 일반적인 스프라이트 시트입니다. 그것은 애니메이션 스프라이트 프레임 시퀀스이거나 같은 장면에서 사용될 이미지 팩이 될 수있다.


Creating from .png and .plist file

CCSpriteSheet 는 1.x  버전이나 그 이하 버전에서 사용되었지만. 2.x 이상 버전에서는 CCSpriteBatchNode 를 사용한다.
CCSpriteBatchNode는 사실상 스프라이트 프레임 이미지 텍스쳐 이다.  너는 반드시 Scene에 이것을 add해야됩니다. 이것은 add를 해도 실제로는 그려지지 않지만
렌더링 파이프 라인의 일부가 됩니다.
예를들면:
1CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::create("animations/grossini.png");

그다음에 너는 CCSpriteFrameCache 싱글톤 클래스를 사용하여 plist 파일을 로드합니다.

1CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
2cache->addSpriteFramesWithFile("animations/grossini.plist"); 

CCSpriteBatchNode와 프레임이로드 및 스프라이트 시트를 장면에 추가되면, 당신은 "createWithSpriteFrameName"방법을 사용하고, CCSpriteBatchNode  자식으로 추가하여 이러한 프레임을 사용 스프라이트를 만들 수 있습니다 :

1m_pSprite1 = CCSprite::createWithSpriteFrameName("grossini_dance_01.png");
2spritebatch->addChild(m_pSprite1);
3addChild(spritebatch);

createWithSpriteFrameName

 이 메서드는  posit 파일을 이용하여 해당되는 사각형 영역을 찾을수 있습니다.

CCArray 오브젝트에 에니메이션 프레임을 더합니다. 에니메이션 사이즈는 14 프레임이고 우리는 이것을 사용하면 됩니다.

 1CCArray* animFrames = CCArray::createWithCapacity(15);
 2
 3char str[100] = {0};
 4
 5for(int i = 1; i < 15; i++) 
 6{
 7    sprintf(str, "grossini_dance_%02d.png", i);
 8    CCSpriteFrame* frame = cache->spriteFrameByName( str );
 9    animFrames->addObject(frame);
10}

마지막으로 우리는 CCAnimate 만들고 CCSprite에 runAction을 해줍니다. CCRepeatForever를 이용해서 무한으로 에니메이션을 돌릴수 있습니다.

1CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.3f);
2m_pSprite1->runAction( CCRepeatForever::create( CCAnimate::create(animation) ) );


File animation

CCAnimationCache can load a xml/plist file which well describes the batch node, sprite frame names and their rectangles. The interfaces are much easier to use.

1CCAnimationCache *cache = CCAnimationCache::sharedAnimationCache(); // "caches" are always singletons in cocos2d
2cache->addAnimationsWithFile("animations/animations-2.plist");
3CCAnimation animation = cache->animationByName("dance_1");  // I apologize for this method name, it should be getAnimationByName(..) in future versions
4CCAnimate animate = CCAnimate::create(animation);  // Don't confused between CCAnimation and CCAnimate :)
5sprite->runAction(animate);


Trackback 0 And Comment 0