2013-07-29 79 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。编码部分对于动画不是必需的。