我正在一个应用程序,它有很多像旋转,平移,缩放基本动画。我是一个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. 使用.png文件的动画cocos2dx
- 2. 使用框架动画精灵cocos2dx 3.2
- 3. 如何创建android cocos2dx启动画面?
- 4. 在Cocos2dx中通过plist文件加载动画
- 5. 在cocos2dx
- 6. cocos2dx相当于Android的画布的BitmapShader
- 7. Cocos2dx动画(不使用过时的方法)
- 8. ccTouchesMoved在cocos2dx
- 9. Cocos2dx Android动态壁纸
- 10. 在“页面”(CCLayer)之间移动cocos2dx
- 11. cocos2dx。如何在动画序列后设置回调(传递参数)
- 12. 如何在cocos2dx v3.7
- 13. 如何cocos2dx在Android
- 14. 崩溃时,在Cocos2dx
- 15. Cocos2dx RegisterTouchDispatcher
- 16. Cocos2dx Revmob
- 17. cocos2dX setBlendFunc
- 18. Cocos2dx sublayers
- 19. 在cocos2dx中使用CCTexture2D
- 20. 在Cocos2dx中使用Spine
- 21. 内存问题在cocos2dx
- 22. 在Cocos2dx中集成OpenGL
- 23. 看不见的AdMob在cocos2dx
- 24. Cocos2dx重新启动场景崩溃CCTextureCache
- 25. cocos2dx“ParticleSystem”移动时效果不正确?
- 26. Cocos2dx,box2d坦克
- 27. Cocos2dx undefined参考
- 28. cocos2dx Sprite setPosition error
- 29. 使用Cocos2dx
- 30. Cocos2dx CCTableView问题
一个好的开始是区分'android'和'ios'。您的应用程序仅适用于这些平台中的一个*,或者两者兼而有之? (后者意味着你的'cocos2d-x'标签无关紧要。) – usr2564301
它适用于平台,iOS和Android。 –
道歉,我看到cocos2d-x是一个跨平台的引擎。只有iOS相当于* cocoa * :) – usr2564301