一个英雄类的创建-创新互联

****************************Hero.h******************************

且末ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

#ifndef __HERO_H__
#define __HERO_H__
#include "cocos2d.h"
//#include "cocos-ext.h"
using namespace cocos2d;
//USING_NS_CC_EXT;
class Hero:public cocos2d::Node
{
public:
   Hero(void);
   ~Hero(void);
   //根据图片名创建英雄
   void InitHeroSprite(char *hero_name);
   //设置动画,num为图片数目,run_directon为精灵脸朝向,false朝右,name_each为name_png中每一小张图片的公共名称部分
   void SetAnimation(const char *name_plist,const char *name_png,const char *name_each,const unsigned int num,bool run_directon);
   //停止动画
   void StopAnimation();
   //判断是否在跑动画
   bool IsRunning;
   //英雄运动的方向
   bool HeroDirecton;
   CREATE_FUNC(Hero);
private:
   Sprite* m_HeroSprite;//精灵
   char *Hero_name;//用来保存初始状态的精灵图片名称
};
#endif // __HERO_H__

*************************************Hero.cpp************************

#include "Hero.h"
USING_NS_CC;
//USING_NS_CC_EXT;
Hero::Hero(void)
{
   IsRunning=false;//没在放动画
   HeroDirecton=false;//向右运动
   Hero_name=NULL;
}

Hero::~Hero(void)
{

}
void Hero::InitHeroSprite(char *hero_name)
{
   Hero_name=hero_name;
   this->m_HeroSprite=Sprite::create(hero_name);
   this->addChild(m_HeroSprite);
}
//动画播放,可以是跑、***、死亡、受伤等
void Hero::SetAnimation(const char *name_plist,const char *name_png,const char *name_each,unsigned int num,bool run_directon)
{
   if(HeroDirecton!=run_directon)
   {  HeroDirecton=run_directon;
   m_HeroSprite->setFlipX(run_directon);
   }
   if(IsRunning)
      return;
   //将图片加载到精灵帧缓存池
   SpriteFrameCache *m_frameCache=SpriteFrameCache::sharedSpriteFrameCache();
   m_frameCache->addSpriteFramesWithFile(name_plist,name_png);
   //用一个列表保存所有的CCSpriteFrameCache
   //CCArray* frameArray= CCArray::createWithCapacity(num);
   Vector frameArray;
   unsigned int i;
   for(i=1;i<=num;i++)
   {
      SpriteFrame* frame=m_frameCache->spriteFrameByName(String::createWithFormat("%s%d.png",name_each,i)->getCString());
      frameArray.pushBack(frame);
   }
   //使用列表创建动画对象
   Animation* animation=Animation::createWithSpriteFrames(frameArray);
   if(HeroDirecton!=run_directon)
   {  HeroDirecton=run_directon;

   }
   animation->setLoops(-1);//表示无限循环播放
   animation->setDelayPerUnit(0.1f);//每两张图片的时间隔,图片数目越少,间隔最小就越小

   //将动画包装成一个动作
   Animate* act=Animate::create(animation);

   m_HeroSprite->runAction(act);
   IsRunning=true;

}
void Hero::StopAnimation()
{
   if(!IsRunning)
      return;
   m_HeroSprite->stopAllActions();//当前精灵停止所有动画

   //恢复精灵原来的初始化贴图
   this->removeChild(m_HeroSprite,TRUE);//把原来的精灵删除掉
   m_HeroSprite=Sprite::create(Hero_name);//恢复精灵原来的贴图样子
   m_HeroSprite->setFlipX(HeroDirecton);
   this->addChild(m_HeroSprite);
   IsRunning=false;
   IsRunning=false;
}


另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:一个英雄类的创建-创新互联
标题链接:http://myzitong.com/article/ccjegi.html