回転、平行移動、スケーリングなどの基本的なアニメーションがたくさんあるアプリに取り組んでいます。私は objC プログラマーであり、cocos やゲーム開発環境での経験がないため、これは私にとって難しくなっています。私はこれについて多くのことをグーグルで調べましたが、あちこちで繰り返される例はほとんど見つかりませんでした. 疑似コードまたは少なくとも基本的なアイデアと、それに付随する説明を教えてください。
11 に答える
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
以下のコードは、スプライトを 1 回移動します。
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));
これらは、さらに返信が必要な場合の基本的なアニメーションです。
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) ) );
したがって、基本的に、画像 (または ) で実行できるアニメーションには 2 つのタイプがあります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 と move->reverse() の組み合わせである新しいアクション/アニメーションを作成します。
CCRepeatForever を使用してアクションを無限に繰り返すか、コンストラクターへの入力パラメーターとして回数を取る CCRepeat を使用できます。
CCScaleBy、CCScaleTo、CCRotateBy、CCRotateTo、これらはすべて同様の方法で使用でき、CCSequence を使用してシーケンスできます。
LevelHelper と SpriteHelper を使用できます。アニメーションにはコーディング部分は必要ありません。
私が Cocos2dx を学び始めたときに経験したことによると、CCSprite をいじるには 2 つの方法があります。
- 更新タイマー + 更新呼び出しごとのインスタント アクション (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);
1秒で移動(-50、-50)。
あなたの親友はテストアプリケーションです。それをコンパイルして実行し、やりたいことに似たものを見つけて、コードをチェックしてください。ソースは cocos2d-x/tests/cpp-tests/Classes/ ( github へのリンク)にあります。
アニメーションに関する例が必要な場合は、アクションに関連するプロジェクト (ActionManagerTest、ActionEaseTest など) を確認できます。
あなたはこのようにすることができます........
//"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) ) );
最初に、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);
あなたが見つけることができる最良の例/サンプルは、Cocos 2dx 自体 Cocos2dxHome->Samples->Cpp->TestCpp->Classes にあります。