我正在一個應用程序,它有很多像旋轉,平移,縮放基本動畫。我是一個objC程序員,並且沒有任何在cocos或遊戲開發環境中的經驗,所以這對我來說很難。我爲此搜索了很多,並發現了幾個重複的例子。任何人都可以用僞碼幫助我,或者至少有一個基本的想法來開始和一些伴隨的解釋來指導我。動畫在Cocos2dx
回答
您可以在Cocos 2dx本身的Cocos2dxHome-> Samples-> Cpp-> TestCpp-> Classes中找到最佳示例/示例。
下面的代碼將再次移動你的精靈:
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));
如果你需要更多的回覆,這些是基本的動畫。
這是接近我正在尋找的東西,謝謝aniket。如果你能幫助我解決更多的問題,那會非常友善。 –
你想要什麼? –
首先你需要一個軟件來製作一個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);
幀動畫在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
ü可以這樣做...........
//"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)));
所以基本上有兩種類型的您可以在圖片上運行的動畫(或ccsprite
)。
1:文: - CCmoveBy
,CCmoveTo
,CCrotateBy
等
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
可以使用以下代碼來左向右連續移動精靈:
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進行測序。
據我經歷了什麼,當我開始學習Cocos2dx,有兩種方法來惹CCSprite各地:每次更新調用(ScheduleUpdate + CCActionInstant)
- 更新計時器+即時操作;
- 行動在有限時間內(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秒
您可以使用簡單的動畫,並通過使用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)));
你最好的朋友是測試應用程序。編譯並運行它,找到類似於你想要做的事情,並檢查它的代碼。您可以在源的cocos2d-x /測試/ CPP-測試/班/(link to github)
如果您需要了解的動畫,你可以檢查與行動(ActionManagerTest,ActionEaseTest等)項目的例子;
您可以使用LevelHelper和SpriteHelper。編碼部分對於動畫不是必需的。
- 1. cocos2dx v3粒子動畫
- 2. cocos2dx 3.13 - 帶動畫的DrawNode
- 3. 使用.png文件的動畫cocos2dx
- 4. 如何創建android cocos2dx啓動畫面?
- 5. 使用框架動畫精靈cocos2dx 3.2
- 6. 在Cocos2dx中通過plist文件加載動畫
- 7. 在cocos2dx
- 8. cocos2dx相當於Android的畫布的BitmapShader
- 9. Cocos2dx動畫(不使用過時的方法)
- 10. 閃爍,同時更改脊椎動畫cocos2dx
- 11. ccTouchesMoved在cocos2dx
- 12. Cocos2dx Android動態壁紙
- 13. 在「頁面」(CCLayer)之間移動cocos2dx
- 14. cocos2dx。如何在動畫序列後設置回調(傳遞參數)
- 15. 崩潰時,在Cocos2dx
- 16. 如何在cocos2dx v3.7
- 17. 如何cocos2dx在Android
- 18. Cocos2dx sublayers
- 19. Cocos2dx RegisterTouchDispatcher
- 20. cocos2dX setBlendFunc
- 21. Cocos2dx Revmob
- 22. 動畫未在matplotlib動畫
- 23. cocos2dx「ParticleSystem」移動時效果不正確?
- 24. Cocos2dx重新啓動場景崩潰CCTextureCache
- 25. 看不見的AdMob在cocos2dx
- 26. 內存問題在cocos2dx
- 27. 在cocos2dx中使用CCTexture2D
- 28. scheduleOnce在cocos2dx V3拉姆達
- 29. 在Cocos2dx中使用Spine
- 30. 在Cocos2dx中集成OpenGL
一個好的開始是區分'android'和'ios'。您的應用程序僅適用於這些平臺中的一個*,或者兩者兼而有之? (後者意味着你的'cocos2d-x'標籤無關緊要。) – usr2564301
它適用於平臺,iOS和Android。 –
道歉,我看到cocos2d-x是一個跨平臺的引擎。只有iOS相當於* cocoa * :) – usr2564301