程序员人生 网站导航

[置顶] Cocos2d-x扣血飘字特效用完你就消失--之游戏开发《赵云要格斗》(8)

栏目:综合技术时间:2015-01-23 09:09:56

        这里是Evankaka的博客,欢迎大家前面讨论与交换~~~~~~

        转载请注明出处http://blog.csdn.net/evankaka/article/details/42740575

       本文这里将要实现的1个功能是怪物受伤血量减少时,会出现1个扣了多少血量的数字从怪物中间飘到头顶,然后消失。然后有两种方式,1种是每次都扣一样的数字,1种是每次在1定范围内的数字随机,本文上接博主的文章http://blog.csdn.net/evankaka/article/details/42689689。

cocos2d-x版本:2.2.5

工程环境:windows7+VS2010

打开方式:将工程放在cocos2d-x安装目录下的project文件夹下用VS打开


效果:



目录

1、自定扣血飘字殊效类FlyWord

2、使用方法

3、思路总结


1、自定扣血飘字殊效类FlyWord

其实这里这里就是1个CCLabelTTF的类型来履行动画CCMoveBy的1个进程

下面来看看我自己定义,头文件FlyWord.h

#ifndef __FLYWORD_H__ #define __FLYWORD_H__ #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; class FlyWord: public CCNode{ public: //创建文字飞舞,create 和 init 是连在1起的,调用create的时候必定会调用init static FlyWord* create(const char *word,const int fontSize,CCPoint begin); //init方法接受3个参数,分别是飘入的字符串,字体大小,从哪里开始飘入,init方法的作用主要是初始化FloatWord内置的Label bool init(const char *word,const int fontSize,CCPoint begin); //文字从下到上飞舞 void Flying(); //文字飞舞后删除所有对象 void Flyend(); private: int _fontSize;//字体大小 CCPoint _begin;//要添加到的怪物的中心点 CCLabelTTF* m_plabel;//字体类型 }; #endif // __FLYWORD_H__
然后是实现类FlyWord.cpp

#include "FlyWord.h" FlyWord* FlyWord::create(const char *word,const int fontSize,CCPoint begin){ FlyWord* ret = new FlyWord(); //这样写更安全1些 if(ret && ret->init(word,fontSize,begin)){ ret->autorelease(); return ret; } CC_SAFE_DELETE(ret);//安全删除 return nullptr; } bool FlyWord::init(const char *word,const int fontSize,CCPoint begin){ if(!CCNode::init()){ return false; } //初始化 _begin = begin; m_plabel = CCLabelTTF::create(word,"Marker Felt",fontSize); //设置色彩 ccColor3B RGB; RGB.r=255; RGB.g=0; RGB.b=0; m_plabel->setColor(RGB); this->addChild(m_plabel); this->setPosition(ccp(begin.x,begin.y)); //初始化完成后就开始飘字了 Flying(); return true; } //文字从下到上飞舞 void FlyWord::Flying() { CCMoveBy* moveact=CCMoveBy::create(0.5f,CCPointMake(0,70));//0.5秒向上移动70 //创建回调动作,文字飞舞完后 CCCallFunc* callFunc=CCCallFunc::create(this,callfunc_selector(FlyWord::Flyend)); //创建连续动作 CCActionInterval* act=CCSequence::create(moveact,callFunc,NULL); //setVisible(true); this->runAction(act); } void FlyWord::Flyend() { //完成以后把该对象从内存中删除掉 this->removeAllChildrenWithCleanup(true); this->removeFromParentAndCleanup(true); }
代码里都有非常详细的说明,这里我就不再说明了

2、使用方法

在Monster.h或Monster.cpp中添加头文件#include "FlyWord.h"

下面是每次随机产生10⑶0的1个要减少的血量,这也是游戏中比较常见的,怪物每次的受伤量可能不1样。

//受伤动画结束 void Monster::HurtEnd() { IsHurt=false; //产生10⑶01个随机掉血的量 srand((UINT)GetCurrentTime()); int x = rand()%30+10; //转成字符 char szName[100] = {0}; sprintf(szName,"-%d",x); //怪物掉血 Monster_xue->setCurrentProgress(Monster_xue->getCurrentProgress()-x); //扣血飘字 FlyWord *wen_zi=FlyWord::create(szName,30,CCPointMake(0,0));//放在当前怪物的(0,0)位置,这里的(0,0)是它的中心,具体可以看看锚点 this->addChild(wen_zi,2); if(Monster_xue->getCurrentProgress()==0) { //播放怪物死亡动画 DeadAnimation("monster_dead",2,MonsterDirecton); } }
如果,想让怪物每次受伤量都1样,很简单,代码为

//受伤动画结束 void Monster::HurtEnd() { IsHurt=false; ////产生10⑶01个随机掉血的量 //srand((UINT)GetCurrentTime()); //int x = rand()%30+10; //转成字符 char szName[100] = {0}; sprintf(szName,"-%d",10); //怪物掉血 Monster_xue->setCurrentProgress(Monster_xue->getCurrentProgress()⑴0); //扣血飘字 FlyWord *wen_zi=FlyWord::create(szName,30,CCPointMake(0,0));//放在当前怪物的(0,0)位置,这里的(0,0)是它的中心,具体可以看看锚点 this->addChild(wen_zi,2); if(Monster_xue->getCurrentProgress()==0) { //播放怪物死亡动画 DeadAnimation("monster_dead",2,MonsterDirecton); } }

1.10⑶0随机掉血。文字从下到上


2.固定掉血,文字从下到上

3、思路总结

          主要是这个文字类的设计,这里我的思路的得到英雄的位置,然后CCLabelTTF对像就从英雄的中间位置开始向上移动,履行动画CCMoveBy的1个进程移动写成后它就自己释放了(removeAllChildrenWithCleanup(true)和removeFromParentAndCleanup(true);)。用完1次就扔了,然后等到怪物掉血了,再用,用完再扔,多方便~游戏开发进程中有时思路真的很重要啊!


    

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐