添加完人物之后接着给人物添加上动作。
我们为hero添加4个动作:attack(由3张图片构成),walk(由2张图片构成),hit(由1张图片构成),dead(由1张图片构成);同样,为enemy添加4个动作:attack(由3张图片构成),walk(由2张图片构成),hit(由1张图片构成),dead(由1张图片构成)。
取名分别为:hero-attack-0.png,hero-attack-1.png,hero-attack-2.png,hero-walk-0.png,hero-walk-1.png,hero-hit-0.png,hero-dead-0.png,enemy-attack-0.png,enemy-attack-1.png,enemy-attack-2.png,enemy-walk-0.png,enemy-walk-1.png,enemy-hit-0.png,enemy-dead-0.png。
其中,hero开头的表示hero的动作;enemy开头的表示enemy的动作。attack表示攻击时的动作,walk表示走路时的动作,hit表示受伤时的动作,dead表示死亡时的动作。这些图片资源如下:
hero-attack-0.png:
hero-attack-1.png:
hero-attack-2.png:
hero-walk-0.png:
hero-walk-1.png:
hero-hit-0.png:
hero-dead-0.png:
enemy-attack-0.png:
enemy-attack-1.png:
enemy-attack-2.png:
enemy-walk-0.png:
enemy-walk-1.png:
enemy-hit-0.png:
enemy-dead-0.png:
然后这些图片都存放在Resources/image/目录下,我们需要为这些图片组合成一些列的动作。下面的代码演示了如何将这些图片组合成8个animation并存放在AnimationCache中:
HelloWorld::init()部分代码:
// hero-attack spriteFrame = CCSpriteFrame::create("images/hero-attack-0.png", CCRectMake(0, 0, 122, 134)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-0.png"); spriteFrame = CCSpriteFrame::create("images/hero-attack-1.png", CCRectMake(0, 0, 111, 134)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-1.png"); spriteFrame = CCSpriteFrame::create("images/hero-attack-2.png", CCRectMake(0, 0, 97, 129)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-2.png"); // hero-walk spriteFrame = CCSpriteFrame::create("images/hero-walk-0.png", CCRectMake(0, 0, 100, 137)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-walk-0.png"); spriteFrame = CCSpriteFrame::create("images/hero-walk-1.png", CCRectMake(0, 0, 96, 137)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-walk-1.png"); // hero-hit spriteFrame = CCSpriteFrame::create("images/hero-hit-0.png", CCRectMake(0, 0, 93, 148)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-hit-0.png"); // hero-dead spriteFrame = CCSpriteFrame::create("images/hero-dead-0.png", CCRectMake(0, 0, 134, 71)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-dead-0.png"); // enemy-attack spriteFrame = CCSpriteFrame::create("images/enemy-attack-0.png", CCRectMake(0, 0, 105, 143)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-0.png"); spriteFrame = CCSpriteFrame::create("images/enemy-attack-1.png", CCRectMake(0, 0, 145, 137)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-1.png"); spriteFrame = CCSpriteFrame::create("images/enemy-attack-2.png", CCRectMake(0, 0, 120, 140)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-2.png"); // enemy-walk spriteFrame = CCSpriteFrame::create("images/enemy-walk-0.png", CCRectMake(0, 0, 125, 145)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-walk-0.png"); spriteFrame = CCSpriteFrame::create("images/enemy-walk-1.png", CCRectMake(0, 0, 100, 119)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-walk-1.png"); // enemy-hit spriteFrame = CCSpriteFrame::create("images/enemy-hit-0.png", CCRectMake(0, 0, 72, 122)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-hit-0.png"); // enemy-dead spriteFrame = CCSpriteFrame::create("images/enemy-dead-0.png", CCRectMake(0, 0, 154, 62)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-dead-0.png"); /** add animation cache **/ // hero-attack auto animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 3; i ++) { auto sfName =String::createWithFormat("hero-attack-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "hero-attack"); // hero-walk animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 2; i ++) { auto sfName =String::createWithFormat("hero-walk-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "hero-walk"); // hero-hit animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 1; i ++) { auto sfName =String::createWithFormat("hero-hit-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "hero-hit"); // hero-dead animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 1; i ++) { auto sfName =String::createWithFormat("hero-dead-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "hero-dead"); // enemy-attack animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 3; i ++) { auto sfName =String::createWithFormat("enemy-attack-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "enemy-attack"); // hero-walk animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 2; i ++) { auto sfName =String::createWithFormat("enemy-walk-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "enemy-walk"); // hero-hit animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 1; i ++) { auto sfName =String::createWithFormat("enemy-hit-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "enemy-hit"); // hero-dead animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 1; i ++) { auto sfName =String::createWithFormat("enemy-dead-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "enemy-dead");
我们在Player中添加一个函数来查看效果:
在Player.h中添加:
void runActionForever(std::string action);
在Player.cpp中添加:
void Player::runActionForever(std::string action) { auto animation = AnimationCache::getInstance()->getAnimation(action); auto animate = Animate::create(animation); this->runAction(RepeatForever::create(animate)); }
然后在HelloWorld::init()最后添加如下代码查看效果:
player->runActionForever("hero-attack"); enemy->runActionForever("enemy-walk");
效果如下:
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" #include "Player.h" USING_NS_CC; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { if ( !Layer::init() ) { return false; } auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); Sprite* background = Sprite::create("images/background.png"); background->setPosition(origin + visibleSize/2); this->addChild(background); SpriteFrame *spriteFrame = CCSpriteFrame::create("images/hero-0.png", CCRectMake(0, 0, 99, 140)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-0.png"); spriteFrame = CCSpriteFrame::create("images/enemy-0.png", CCRectMake(0, 0, 112, 136)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-0.png"); // hero-attack spriteFrame = CCSpriteFrame::create("images/hero-attack-0.png", CCRectMake(0, 0, 122, 134)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-0.png"); spriteFrame = CCSpriteFrame::create("images/hero-attack-1.png", CCRectMake(0, 0, 111, 134)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-1.png"); spriteFrame = CCSpriteFrame::create("images/hero-attack-2.png", CCRectMake(0, 0, 97, 129)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-2.png"); // hero-walk spriteFrame = CCSpriteFrame::create("images/hero-walk-0.png", CCRectMake(0, 0, 100, 137)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-walk-0.png"); spriteFrame = CCSpriteFrame::create("images/hero-walk-1.png", CCRectMake(0, 0, 96, 137)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-walk-1.png"); // hero-hit spriteFrame = CCSpriteFrame::create("images/hero-hit-0.png", CCRectMake(0, 0, 93, 148)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-hit-0.png"); // hero-dead spriteFrame = CCSpriteFrame::create("images/hero-dead-0.png", CCRectMake(0, 0, 134, 71)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-dead-0.png"); // enemy-attack spriteFrame = CCSpriteFrame::create("images/enemy-attack-0.png", CCRectMake(0, 0, 105, 143)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-0.png"); spriteFrame = CCSpriteFrame::create("images/enemy-attack-1.png", CCRectMake(0, 0, 145, 137)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-1.png"); spriteFrame = CCSpriteFrame::create("images/enemy-attack-2.png", CCRectMake(0, 0, 120, 140)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-2.png"); // enemy-walk spriteFrame = CCSpriteFrame::create("images/enemy-walk-0.png", CCRectMake(0, 0, 125, 145)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-walk-0.png"); spriteFrame = CCSpriteFrame::create("images/enemy-walk-1.png", CCRectMake(0, 0, 100, 119)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-walk-1.png"); // enemy-hit spriteFrame = CCSpriteFrame::create("images/enemy-hit-0.png", CCRectMake(0, 0, 72, 122)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-hit-0.png"); // enemy-dead spriteFrame = CCSpriteFrame::create("images/enemy-dead-0.png", CCRectMake(0, 0, 154, 62)); SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-dead-0.png"); /** add animation cache **/ // hero-attack auto animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 3; i ++) { auto sfName =String::createWithFormat("hero-attack-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "hero-attack"); // hero-walk animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 2; i ++) { auto sfName =String::createWithFormat("hero-walk-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "hero-walk"); // hero-hit animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 1; i ++) { auto sfName =String::createWithFormat("hero-hit-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "hero-hit"); // hero-dead animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 1; i ++) { auto sfName =String::createWithFormat("hero-dead-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "hero-dead"); // enemy-attack animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 3; i ++) { auto sfName =String::createWithFormat("enemy-attack-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "enemy-attack"); // hero-walk animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 2; i ++) { auto sfName =String::createWithFormat("enemy-walk-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "enemy-walk"); // hero-hit animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 1; i ++) { auto sfName =String::createWithFormat("enemy-hit-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "enemy-hit"); // hero-dead animation = Animation::create(); animation->setDelayPerUnit(0.2f); for (int i = 0; i < 1; i ++) { auto sfName =String::createWithFormat("enemy-dead-%d.png", i)->getCString(); animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(sfName)); } AnimationCache::getInstance()->addAnimation(animation, "enemy-dead"); Player* player = Player::create(Player::PlayerType::HERO); player->setPosition(origin.x + player->getContentSize().width/2, origin.y + visibleSize.height/2); this->addChild(player); Player* enemy = Player::create(Player::PlayerType::ENEMY); enemy->setPosition(origin.x + visibleSize.width - player->getContentSize().width/2, origin.y + visibleSize.height/2); this->addChild(enemy); player->runActionForever("hero-attack"); enemy->runActionForever("enemy-walk"); return true; } void HelloWorld::menuCloseCallback(Ref* pSender) { Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }
#ifndef __Palyer__ #define __Player__ #include "cocos2d.h" USING_NS_CC; class Player : public Sprite { public: enum PlayerType { HERO, ENEMY }; bool initWithPlayerType(PlayerType type); static Player* create(PlayerType type); void runActionForever(std::string action); }; #endif
#include "Player.h" bool Player::initWithPlayerType(PlayerType type) { std::string spName = ""; switch (type) { case PlayerType::HERO: spName = "hero-0.png"; break; case PlayerType::ENEMY: spName = "enemy-0.png"; break; } this->initWithSpriteFrameName(spName); return true; } Player* Player::create(PlayerType type) { Player* player = new Player(); if (player && player->initWithPlayerType(type)) { player->autorelease(); return player; } else { delete player; player = NULL; return NULL; } } void Player::runActionForever(std::string action) { auto animation = AnimationCache::getInstance()->getAnimation(action); auto animate = Animate::create(animation); this->runAction(RepeatForever::create(animate)); }