2013-07-29 182 views
0

我正在一個應用程序,它有很多像旋轉,平移,縮放基本動畫。我是一個objC程序員,並且沒有任何在cocos或遊戲開發環境中的經驗,所以這對我來說很難。我爲此搜索了很多,並發現了幾個重複的例子。任何人都可以用僞碼幫助我,或者至少有一個基本的想法來開始和一些伴隨的解釋來指導我。動畫在Cocos2dx

+0

一個好的開始是區分'android'和'ios'。您的應用程序僅適用於這些平臺中的一個*,或者兩者兼而有之? (後者意味着你的'cocos2d-x'標籤無關緊要。) – usr2564301

+0

它適用於平臺,iOS和Android。 –

+0

道歉,我看到cocos2d-x是一個跨平臺的引擎。只有iOS相當於* cocoa * :) – usr2564301

回答

0

您可以在Cocos 2dx本身的Cocos2dxHome-> Samples-> Cpp-> TestCpp-> Classes中找到最佳示例/示例。

2

下面的代碼將再次移動你的精靈:

CCSprite *sprite=CCSprite::create("image.png"); 
CCMoveTo *moveSprite=CCMoveTo::create(0.5, ccp(200, 400)); 
sprite->runAction(moveSprite); 

下面的線將擴展你的精靈:

sprite->setScale(1.2); 

下面的代碼會旋轉你的精靈:

CCRotateBy *rotate = CCRotateBy::create(0.8f, 360.0f); 
sprite->runAction(CCRepeat::create(rotate, 5)); 

如果你需要更多的回覆,這些是基本的動畫。

+0

這是接近我正在尋找的東西,謝謝aniket。如果你能幫助我解決更多的問題,那會非常友善。 –

+0

你想要什麼? –

0

首先你需要一個軟件來製作一個plist文件。當你得到plist文件時,你可以使用這段代碼來製作一個動畫。

CCSprite *pBody = CCSprite::createWithSpriteFrameName("enemy1_m_1.png"); 
CC_BREAK_IF(!pBody); 

CCSpriteFrameCache* pAttac_FrameCache = CCSpriteFrameCache::sharedSpriteFrameCache(); 
pAttac_FrameCache->addSpriteFramesWithFile("ZombieAttack.plist",CCTextureCache::sharedTextureCache()->addImage("ZombieAttack.png")); 
CCAnimation *pAttac_Animation = CCAnimation::create(); 
pAttac_Animation->setDelayPerUnit(0.1f); 
pAttac_Animation->setLoops(-1); 
int nIndeies = 0;     //Format Name of Picture index 

while(true){ 
    char szFrameName[_FILE_NAME_LEN_] = {0}; 
    sprintf(szFrameName,"ZombieAttack_%d.png",nIndeies++); 
    CCSpriteFrame* pFrame = pAttac_FrameCache->spriteFrameByName(szFrameName); 
    CC_BREAK_IF(pFrame ==NULL); 
    pAttac_Animation->addSpriteFrame(pFrame); 
} 
pBody->runAction(pAttac_Animation); 
2

幀動畫在Cocos2dX

CCAnimation *animation = CCAnimation::create(); 

// load image file from local file system to CCSpriteFrame, 
then add into CCAnimation 

for (int i = 1; i < 15; i++) 

{ 
char szImageFileName[128] = {0}; 

    sprintf(szImageFileName, "Images/grossini_dance_%02d.png", i); 
    animation->addSpriteFrameWithFileName(szImageFileName); 
} 

animation->setDelayPerUnit(2.8f/14.0f); // This animation contains 14 frames, will  continuous 2.8 seconds. 

animation->setRestoreOriginalFrame(true); // Return to the 1st frame after the 14th frame is played. 

CCAnimate *action = CCAnimate::create(animation); 

sprite->runAction(action); // run action on sprite object 
0

ü可以這樣做...........

//"horse.png" through which batch node crate 

    CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::create("horse.png"); 
    CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); 
    cache->addSpriteFramesWithFile("horse.plist"); 

    // "hero" is CCSprite and "horse_1.png" is a sprite in "horse.png" batch node 

    hero = CCSprite::createWithSpriteFrameName("horse_1.png"); 
    addChild(spritebatch); 
    spritebatch->addChild(hero); 

    CCLog("In the anim2"); 

    CCArray* animFrames = CCArray::createWithCapacity(16); 
    char str[100] = {0}; 
    for(int i = 1; i < 16; i++) 
    { 
     // in my batch node all the sprite name as 'horse_1.png', 'horse_2.png'..... 
     sprintf(str, "horse_%i.png", i); 
     CCSpriteFrame* frame = cache->spriteFrameByName(str); 
     animFrames->addObject(frame); 
    } 
    CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, .1f); 
    animation->setRestoreOriginalFrame(true); 
    hero->runAction(CCRepeatForever::create(CCAnimate::create(animation))); 
1

所以基本上有兩種類型的您可以在圖片上運行的動畫(或ccsprite)。

1:文: - CCmoveByCCmoveToCCrotateBy

2:幀動畫。

有用法如下...

CCSprite *sprite=CCSprite::create("image.png"); 

CCMoveTo *moveSprite=CCMoveTo::create(0.5, ccp(200, 400)); 
sprite->runAction(moveSprite); 

CCAnimation *animation = CCAnimation::create(); 

// load image file from local file system to CCSpriteFrame, 
then add into CCAnimation 

for (int i = 1; i < 15; i++) 

{ 
char szImageFileName[128] = {0}; 

    sprintf(szImageFileName, "Images/grossini_dance_%02d.png", i); 
    animation->addSpriteFrameWithFileName(szImageFileName); 
} 

animation->setDelayPerUnit(2.8f/14.0f); // This animation contains 14 frames, will  continuous 2.8 seconds. 

CCAnimate *action = CCAnimate::create(animation); 

sprite->runAction(action); // run action on sprite object 

或..

sprite->runAction(ccRepeatForever::create(action)); 

你應該知道的moveTo,moveBy或ccRepeatForever他們都是子類ccAction

0

可以使用以下代碼來左向右連續移動精靈:

CCSprite* mySprite=CCSprite::create("menuBtn.png"); 
this->addChild(mySprite,1); 
CCActionInterval* move=CCMoveBy::create(0.5,ccp(30,0)); 
mySprite->runAction(CCRepeatForever::create(CCSequence::create(move,move->reverse(),NULL))); 

CCSequence創建一個新的動作/動畫其是移動的組合以及MOVE->反向()。

您可以使用CCRepeatForever無限重複某個操作或使用CCRepeat,該CCRepeat將其次數作爲輸入參數傳遞給其構造函數。

CCScaleBy,CCScaleTo,CCRotateBy,CCRotateTo,所有這些可以以類似的方式被使用,他們可以用CCSequence進行測序。

0

據我經歷了什麼,當我開始學習Cocos2dx,有兩種方法來惹CCSprite各地:每次更新調用(ScheduleUpdate + CCActionInstant)

  1. 更新計時器+即時操作;
  2. 行動在有限時間內(CCActionInterval)

爲1,模板代碼應該是這樣的:

CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(NewGame::update),this,0.1,false); 

NewGame::update(float dt) 
{ 
    ... 
    //subClass of CCActionInstant 
    if(sprite-> isFlipX()) 
    sprite->runAction(CCFlipX::create(true)); 
    else 
    sprite->runAction(CCFlipX::create(false)); 
    ... 
} 

This code will make sprite to change orientation per 0.1 sec. 

爲2,有許多示例代碼和所有的風格極爲相似:

CCActionInterval* actionMoveBy = CCMoveBy::actionWithDuration(1,ccp(-50,-50)); 
m_Soldier->runAction(actionMoveTo); 

移動(-50,-50)在1秒

1

您可以使用簡單的動畫,並通過使用cocos2dx一些複雜的動畫。一些第三方工具可用於製作精靈幀,您可以使用cocos2dx的代碼在這些幀上運行動畫。

CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::create("horse.png"); 
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); 
cache->addSpriteFramesWithFile("horse.plist"); 

// "hero" is CCSprite and "horse_1.png" is a sprite in "horse.png" batch node 

hero = CCSprite::createWithSpriteFrameName("horse_1.png"); 
addChild(spritebatch); 
spritebatch->addChild(hero); 

CCLog("In the anim2"); 

CCArray* animFrames = CCArray::createWithCapacity(16); 
char str[100] = {0}; 
for(int i = 1; i < 16; i++) 
{ 
    // in my batch node all the sprite name as 'horse_1.png', 'horse_2.png'..... 
    sprintf(str, "horse_%i.png", i); 
    CCSpriteFrame* frame = cache->spriteFrameByName(str); 
    animFrames->addObject(frame); 
} 
CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, .1f); 
animation->setRestoreOriginalFrame(true); 
hero->runAction(CCRepeatForever::create(CCAnimate::create(animation))); 
0

你最好的朋友是測試應用程序。編譯並運行它,找到類似於你想要做的事情,並檢查它的代碼。您可以在源的cocos2d-x /測試/ CPP-測試/班/(link to github

如果您需要了解的動畫,你可以檢查與行動(ActionManagerTest,ActionEaseTest等)項目的例子;

0

您可以使用LevelHelper和SpriteHelper。編碼部分對於動畫不是必需的。