添加完人物之后接着给人物添加上动作。
我们为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(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-0.png");
spriteFrame = CCSpriteFrame::create("images/hero-attack-1.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-1.png");
spriteFrame = CCSpriteFrame::create("images/hero-attack-2.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-2.png");
// hero-walk
spriteFrame = CCSpriteFrame::create("images/hero-walk-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-walk-0.png");
spriteFrame = CCSpriteFrame::create("images/hero-walk-1.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-walk-1.png");
// hero-hit
spriteFrame = CCSpriteFrame::create("images/hero-hit-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-hit-0.png");
// hero-dead
spriteFrame = CCSpriteFrame::create("images/hero-dead-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-dead-0.png"); // enemy-attack
spriteFrame = CCSpriteFrame::create("images/enemy-attack-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-0.png");
spriteFrame = CCSpriteFrame::create("images/enemy-attack-1.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-1.png");
spriteFrame = CCSpriteFrame::create("images/enemy-attack-2.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-2.png");
// enemy-walk
spriteFrame = CCSpriteFrame::create("images/enemy-walk-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-walk-0.png");
spriteFrame = CCSpriteFrame::create("images/enemy-walk-1.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-walk-1.png");
// enemy-hit
spriteFrame = CCSpriteFrame::create("images/enemy-hit-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-hit-0.png");
// enemy-dead
spriteFrame = CCSpriteFrame::create("images/enemy-dead-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-dead-0.png"); /** add animation cache **/
// hero-attack
auto animation = Animation::create();
animation->setDelayPerUnit(0.2f);
for (int i = ; i < ; 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 = ; i < ; 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 = ; i < ; 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 = ; i < ; 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 = ; i < ; 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 = ; i < ; 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 = ; i < ; 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 = ; i < ; 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/);
this->addChild(background); SpriteFrame *spriteFrame = CCSpriteFrame::create("images/hero-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-0.png");
spriteFrame = CCSpriteFrame::create("images/enemy-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-0.png");
// hero-attack
spriteFrame = CCSpriteFrame::create("images/hero-attack-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-0.png");
spriteFrame = CCSpriteFrame::create("images/hero-attack-1.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-1.png");
spriteFrame = CCSpriteFrame::create("images/hero-attack-2.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-attack-2.png");
// hero-walk
spriteFrame = CCSpriteFrame::create("images/hero-walk-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-walk-0.png");
spriteFrame = CCSpriteFrame::create("images/hero-walk-1.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-walk-1.png");
// hero-hit
spriteFrame = CCSpriteFrame::create("images/hero-hit-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-hit-0.png");
// hero-dead
spriteFrame = CCSpriteFrame::create("images/hero-dead-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "hero-dead-0.png"); // enemy-attack
spriteFrame = CCSpriteFrame::create("images/enemy-attack-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-0.png");
spriteFrame = CCSpriteFrame::create("images/enemy-attack-1.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-1.png");
spriteFrame = CCSpriteFrame::create("images/enemy-attack-2.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-attack-2.png");
// enemy-walk
spriteFrame = CCSpriteFrame::create("images/enemy-walk-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-walk-0.png");
spriteFrame = CCSpriteFrame::create("images/enemy-walk-1.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-walk-1.png");
// enemy-hit
spriteFrame = CCSpriteFrame::create("images/enemy-hit-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-hit-0.png");
// enemy-dead
spriteFrame = CCSpriteFrame::create("images/enemy-dead-0.png", CCRectMake(, , , ));
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, "enemy-dead-0.png"); /** add animation cache **/
// hero-attack
auto animation = Animation::create();
animation->setDelayPerUnit(0.2f);
for (int i = ; i < ; 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 = ; i < ; 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 = ; i < ; 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 = ; i < ; 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 = ; i < ; 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 = ; i < ; 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 = ; i < ; 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 = ; i < ; 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/, origin.y + visibleSize.height/);
this->addChild(player);
Player* enemy = Player::create(Player::PlayerType::ENEMY);
enemy->setPosition(origin.x + visibleSize.width - player->getContentSize().width/, origin.y + visibleSize.height/);
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();
#endif
}

HellWorldScene.cpp

#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

Player.h

#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));
}

Player.cpp

cocos2dx游戏--欢欢英雄传说--添加动作的更多相关文章

  1. cocos2dx游戏--欢欢英雄传说--添加游戏背景

    经过一段时间的学习cocos2dx,接下来我想要实践开发一个小游戏,我把它命名为“欢欢英雄传说”,项目名将取为HuanHero.环境:cocos2dx环境:cocos2d-x 3.11.1IDE:Co ...

  2. cocos2dx游戏--欢欢英雄传说--添加攻击按钮

    接下来添加攻击按钮用于执行攻击动作.同时修复了上一版移动时的bug.修复后的Player::walkTo()函数: void Player::walkTo(Vec2 dest) { if (_seq) ...

  3. cocos2dx游戏--欢欢英雄传说--添加人物

    接下来需要导入精灵帧资源,因为之前下载了TexturePacker,然后通过TexturePacker的"Publish sprite sheet"方法可以生成一个.pvr.ccz ...

  4. cocos2dx游戏--欢欢英雄传说--添加触摸响应

    主要的调整就是将HelloWorldScene改成了MainSecne,然后将Player作为了MainScene的私有成员变量来处理.修改了人物图片,使用了网上找到的三国战纪的人物素材代替我之前画的 ...

  5. cocos2dx游戏--欢欢英雄传说--为敌人添加移动和攻击动作

    这里主要为敌人添加了一个移动动作和攻击动作.移动动作是很简略的我动他也动的方式.攻击动作是很简单的我打他也打的方式.效果:代码: #ifndef __Progress__ #define __Prog ...

  6. cocos2dx游戏--欢欢英雄传说--添加血条

    用一个空血槽图片的Sprite做背景,上面放一个ProgressTimer, 通过设置ProgressTimer的进度来控制血条的长短.建立一个Progress类来实现.Progress.h: #if ...

  7. Learning Cocos2d-x for WP8(8)——动作Action

    原文:Learning Cocos2d-x for WP8(8)--动作Action 游戏很大程度上是由动作画面支撑起来的. 动作分为两大类:瞬间动作和延时动作. 瞬间动作基本等同于设置节点的属性,延 ...

  8. cocos2d-x游戏引擎核心之九——跨平台

    一.cocos2d-x跨平台 cocos2d-x到底是怎样实现跨平台的呢?这里以Win32和Android为例. 1. 跨平台项目目录结构 先看一下一个项目创建后的目录结构吧!这还是以HelloCpp ...

  9. cocos2d-x游戏引擎核心之三——主循环和定时器

    一.游戏主循环 在介绍游戏基本概念的时候,我们曾介绍了场景.层.精灵等游戏元素,但我们却故意避开了另一个同样重要的概念,那就是游戏主循环,这是因为 Cocos2d 已经为我们隐藏了游戏主循环的实现.读 ...

随机推荐

  1. 【Unity】物体跟随鼠标移动

    需求:2D游戏中,需要物体跟随鼠标移动. 做法:其实思路也很简单,就是先获取到鼠标的坐标,然后赋值给目标物体即可. void Update(){ // 物体跟随鼠标移动 Vector2 mousePo ...

  2. Windows C++ 非递归式(stack)深度优先遍历目录

    #include <Windows.h> #include <cstdio> #include <cstring> #include <string> ...

  3. shell中执行hive命令错误:delimited by end-of-file (wanted `EOF')

    错误信息: warning: here-document at line 58 delimited by end-of-file (wanted `EOF') 业务场景,使用hive对数据进行批量清洗 ...

  4. java-JSP脚本的9个内置对象

    http://blog.csdn.net/titilover/article/details/6800782 http://www.importnew.com/19128.html http://ww ...

  5. 受打击了:你是学.net 的吧?

    我在网上投了简历,今天去面试, 去到才知道有面试题做,做完之后自知答的很烂. 没想到面试我的那个人,一开始就很直接,说: 我感觉你很喜欢用英语, 但英语很烂 我觉得你很喜欢用别人的东西, 但技术水平很 ...

  6. 更改HDFS权限

    hdfs dfs -chmod -R 755 / 之前执行过这条语句,但是总是提示: 15/05/21 08:10:18 WARN util.NativeCodeLoader: Unable to l ...

  7. android 内存管理机制、异常、垃圾回收

    当 Android 应用程序退出时,并不清理其所占用的内存,Linux 内核进程也相应的继续存在,所谓“退出但不关闭”.从而使得用户调用程序时能够在第一时间得到响应. 当系统内存不足时,系统将激活内存 ...

  8. thinkphp 前台测试

    配置文件 <?php return array( 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 ...

  9. 【转】BMP图像文件格式

    5.1  BMP图像文件格式 BMP图像文件格式是游戏中常用的图像资源文件格式,BMP图像文件起源早,程序员对BMP都比较熟悉,再加上BMP格式简单,读取和写入非常容易实现,所以无论Windows的还 ...

  10. 第二百八十九节,MySQL数据库-ORM之sqlalchemy模块操作数据库

    MySQL数据库-ORM之sqlalchemy模块操作数据库 sqlalchemy第三方模块 sqlalchemysqlalchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API ...