我正在开发一款具有很多基本动画的应用,例如旋转,平移,缩放。我是一名objC程序员,在cocos或游戏开发环境中没有任何经验,因此这对我来说很难。我已经搜索了很多,并发现了几个重复的例子。任何人都可以帮助我使用伪代码或至少一个基本的想法开始和一些附带的解释来指导我。
答案 0 :(得分: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));
如果您需要更多回复,这些是基本动画。
答案 1 :(得分: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
答案 2 :(得分:1)
因此,您可以在图片上运行两种类型的动画(或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的子类
答案 3 :(得分: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) ) );
答案 4 :(得分:0)
您可以找到的最佳实例/样品位于Cocos 2dx本身Cocos2dxHome-&gt; Samples-&gt; Cpp-&gt; TestCpp-&gt; Classes。
答案 5 :(得分: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);
答案 6 :(得分: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) ) );
答案 7 :(得分: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和move-&gt; reverse()的组合。
您可以使用CCRepeatForever无限重复某个操作,或者使用CCRepeat作为其构造函数的输入参数。
CCScaleBy,CCScaleTo,CCRotateBy,CCRotateTo,所有这些都可以以类似的方式使用,并且可以使用CCSequence对它们进行排序。
答案 8 :(得分:0)
根据我开始学习Cocos2dx时的经历,有两种方法可以解决CCSprite:
对于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)
答案 9 :(得分:0)
你最好的朋友是测试应用程序。编译并运行它,找到类似于你想要做的事情,并检查它的代码。你可以在cocos2d-x / tests / cpp-tests / Classes /(link to github)找到源代码
如果您需要有关动画的示例,可以检查与动作相关的项目(ActionManagerTest,ActionEaseTest等);
答案 10 :(得分:0)
您可以使用LevelHelper和SpriteHelper。动画不需要编码部分。