繁体   English   中英

Cocos2dx中的动画

[英]animations in Cocos2dx

我正在开发一个具有很多基本动画(例如旋转,平移,缩放)的应用程序。 我是objC程序员,没有任何关于cocos或游戏开发环境的经验,因此这对我来说很难。 我为此进行了大量的搜索,并发现了一些重复出现的示例。 任何人都可以通过伪代码或至少一个基本的概念帮助我,并提供一些附带的指导来指导我。

下面的代码将移动您的精灵一次:

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中的帧动画

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

因此,基本上可以在图像(或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的子类。

您可以使用cocos2dx使用简单的动画和一些复杂的动画。 一些第三方工具可用于制作Sprite框架,您可以使用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) ) );

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

首先,您需要一个软件来制作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);

你可以这样...

//"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* 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对其进行排序。

根据我开始学习Cocos2dx时的经历,有两种方法可以弄乱CCSprite:

  1. 更新计时器+每个更新调用的即时操作(ScheduleUpdate + CCActionInstant);
  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);

在1秒钟内移动(-50,-50)。

您最好的朋友是测试应用程序。 编译并运行它,找到与您想要执行的操作类似的操作并检查其代码。 您可以在cocos2d-x / tests / cpp-tests / Classes /中找到源代码( 链接到github

如果您需要有关动画的示例,则可以检查与动作相关的项目(ActionManagerTest,ActionEaseTest等);

您可以使用LevelHelper和SpriteHelper。 动画不需要编码部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM