[cocos2d-x 2.0+遊戲開發]動畫的創建

歡迎轉載。轉載請註明softboy 博客: http://www.softboy.uqc.cn

第一次開發cocos2d-x 的遊戲.一邊學習一遍進步,記錄成博客 歡迎大家一起分析.
等我開發出一款精美遊戲,在回首,將是一個美好的記憶.
註明,本教程都是cocos2d-2.0-rc2-x-2.0.1  較以前有較大的改變,以前的版本的教程真的是隻能做參考.

    這個是動畫初始化.我的圖是500*200的 .
CCAnimation* AnimationManager::createNPCAnimation()
{
	CCTexture2D *playerRunTexture = CCTextureCache::sharedTextureCache()->addImage("jumping.png");      
    CCAnimation* animation = CCAnimation::create();  
    for( int i = 0;i < 5;i++){  
        animation->addSpriteFrame(CCSpriteFrame::create(playerRunTexture, cocos2d::CCRectMake(100*i, 0, 100, 100)));  
    }  
	 for( int i = 0;i < 5;i++){  
        animation->addSpriteFrame(CCSpriteFrame::create(playerRunTexture, cocos2d::CCRectMake(100*i, 100, 100, 100)));  
    }  
    // should last 2.8 seconds. And there are 14 frames.  
    animation->setDelayPerUnit(0.2f);
	animation->setRestoreOriginalFrame(true);
	return animation;
}


這個時候 就可以 通過下面代碼 把動畫加載hello world 上面了 .
CCSprite *m_grossini;
m_grossini = CCSprite::create("npc.png");
		m_grossini->setPosition(CCPointMake(100,100));
		addChild(m_grossini, 1);
m_grossini->setVisible(true);
    m_grossini->retain();
    CCAnimate* action = sAnimationMgr->createAnimate("1");//CCAnimate::create(sAnimationMgr->createAnimate("1"));
    m_grossini->runAction(CCSequence::create(action, action->reverse(), NULL));



通過名字管理動畫.,爲了方便管理動畫 .



一般是把動畫加到 CCAnimationCache 中.
CCAnimationCache::sharedAnimationCache()->addAnimation(createNPCAnimation(), "1");

這樣,每次使用動畫 就用;
CCAnimation* anim = CCAnimationCache::sharedAnimationCache()->animationByName(key);
就好了 .

運行的圖就不上了 .
發佈了47 篇原創文章 · 獲贊 15 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章