cocos2d -x 技能按键实现

自己写了一个技能栏的工具,希望对各位游戏开发者有用。部分函数使用了我上一篇文章中的hashtable。

测试代码如下,附件中为测试图片,部分图片来自网络,仅供学习参考,请勿商用,后果自负。

CCSprite *normal=CCSprite::spriteWithFile("fire_normal.png");
CCSprite *select=CCSprite::spriteWithFile("fire_press.png");
    CCSprite *testselectbg=CCSprite::spriteWithFile("r1-hd.png");
testselectbg->setOpacity(100);
SkillBar *sb=SkillBar::createSkillBar(normal,select,testselectbg,10.0f,CCCallFuncN::actionWithTarget(this,callfuncN_selector(HelloWorld::menuCloseCallback)));
sb->addSkillBar(CCSprite::spriteWithFile("fire_press.png"),CCSprite::spriteWithFile("fire_normal.png"),CCSprite::spriteWithFile("r1-hd.png"),4.0f,CCCallFuncN::actionWithTarget(this,callfuncN_selector(HelloWorld::menuCloseCallback)));
sb->addSkillBar(CCSprite::spriteWithFile("fire_press.png"),CCSprite::spriteWithFile("fire_normal.png"),CCSprite::spriteWithFile("r1-hd.png"),8.0f,CCCallFuncN::actionWithTarget(this,callfuncN_selector(HelloWorld::menuCloseCallback)));
this->addChild(sb);
CCSprite * gamebg=CCSprite::spriteWithTexture(CCTextureCache::sharedTextureCache()->textureForKey(png_playbg));
sb->setSkillBarBg(gamebg);
sb->setPosition(ccp(0,screenSize.height/2));


//-------------------------------------------------------------------------------
#ifndef SKILLBUTTON_H_
#define SKILLBUTTON_H_

#include "cocos2d.h"
using namespace cocos2d;

class SkillBar :public CCLayer
{
private:
CCSprite * coolbg;//冷却半透明
CCProgressTo *action_progress;//进度动作
CCProgressTimer *progressTimer;//进度条
CCMenuItemSprite * skill_item;//技能按钮
CCMenu *skills;//技能

int count;//记录技能个数
float cellwidth;
float cellheight;

public:
//创建一个技能按钮
static SkillBar * createSkillBar(CCSprite *normal,CCSprite *select,CCSprite *bg,float durtaion,CCCallFunc *callback );

void setSkillBarBg(CCSprite *bg);
//初始化技能条
void initSkillBar(CCSprite *normal,CCSprite *select,CCSprite *bg,float durtaion,CCCallFunc* callback );
//添加一个技能按钮
void addSkillBar(CCSprite *normal,CCSprite *select,CCSprite *bg,float durtaion,CCCallFunc* callback);
private:
//技能点击后的回调
void SkillClickCallBack(CCObject* object);
//技能冷却完成时的回调
void SkillCoolDownCallBack(CCNode* node,void *object);

};
#endif





//-------------------------------------------------------------------------------



#include "SkillBar.h"
//工具类
//--------------------------------------------------------------------------------
static std::map<std::string,std::string> datamap;

/*
*@description: 存入一个string类型的数据
*@author: buerkai.iteye.com
*@fullName: put
*@param1: key
*@param2: value
*/
static void put(std::string key,std::string value)
{
if(datamap.find(key)!=datamap.end()){
datamap.find(key)->second=value;
}else{
datamap.insert(make_pair(key,value));
}

}


/*
*@description: 取一个string类型的数据,没有返回为空串
*@author: buerkai.iteye.com
*@fullName: get
*@param1: key
*/
static std::string get(std::string key)
{
if(datamap.find(key)!=datamap.end()){
return datamap.find(key)->second;
}else{
return "";
}
}


/*
*@description: 删除string类型的数据
*@author: buerkai.iteye.com
*@fullName: deleteValue
*@param1: key
*/
static void deleteValue(std::string key){
if(datamap.find(key)!=datamap.end()){
datamap.erase(key);
}
}

/*
*@description: 存入一个Int类型的数据
*@author: buerkai.iteye.com
*@fullName: put
*@param1: key
*@param2: value
*/
static void putInt(std::string key,int value)
{
char tt[10];
put(key,itoa(value,tt,10));
}

/*
*@description: 取一个Int类型的数据,没有返回0
*@author: buerkai.iteye.com
*@fullName: getInt
*@param1: key
*/
static int getInt(std::string key)
{
std::string dd=get( key);
if(dd==""){
return 0;
}else{
return std::atoi(dd.c_str());
}
}

/*
*@description: 存入一个Float类型的数据
*@author: buerkai.iteye.com
*@fullName: putFloat
*@param1: key
*@param2: value
*/
static void putFloat(std::string key,float value)
{
char tmpstr[32];
memset(tmpstr,'\0',32);
    sprintf(tmpstr,"%f",value);
put(key,std::string(tmpstr));
}


/*
*@description: 取一个Float类型的数据,没有返回0.0f
*@author: buerkai.iteye.com
*@fullName: getFloat
*@param1: key
*/
static float getFloat(std::string key)
{
std::string dd=get( key);
if(dd==""){
return 0.0f;
}else{
return std::atof(dd.c_str());
}
}



/*
*@description: 存入一个bool类型的数据
*@author: buerkai.iteye.com
*@fullName: putBool
*@param1: key
*@param2: value
*/
static void putBool(std::string key,bool value)
{
std::string tt="0";
if(value)
{
tt="1";
}
put(key,std::string(tt));
}



/*
*@description: 取一个bool类型的数据,没有返回false
*@author: buerkai.iteye.com
*@fullName: getBool
*@param1: key
*/
static bool getBool(std::string key)
{
std::string dd=get( key);
if(dd==""||dd=="0"){
return 0;
}else if(dd=="1"){
return 1;
}
return 0;
}
//--------------------------------------------------------------------------------
//
/*
*@description: 创建一个技能条
*@author: buerkai.iteye.com
*@fullName: SkillBar::createSkillBar
*@return: SkillBar
*@param1: normal,技能按键正常情况的sprite
*@param2: select,技能按键选中情况的sprite
*@param3: bg,技能按键冷却情况的sprite
*@param4: durtaion,冷却时间
*@param5: callback,点击技能按键后的回调函数
*/
SkillBar * SkillBar::createSkillBar(CCSprite *normal,CCSprite *select,CCSprite *bg,float durtaion,CCCallFunc* callback ){
SkillBar *sb=new SkillBar();
sb->initSkillBar(normal,select,bg,durtaion,callback);
return sb;
}



/*
*@description: 初始化技能条
*@author: buerkai.iteye.com
*@fullName: SkillBar::initSkillBar
*@return: SkillBar
*@param1: normal,技能按键正常情况的sprite
*@param2: select,技能按键选中情况的sprite
*@param3: bg,技能按键冷却情况的sprite
*@param4: durtaion,冷却时间
*@param5: callback,点击技能按键后的回调函数
*/
void SkillBar::initSkillBar(CCSprite *normal,CCSprite *select,CCSprite *bg,float durtaion,CCCallFunc* callback ){
count=0;
skill_item=CCMenuItemSprite::itemWithNormalSprite(normal,select,normal,this,menu_selector(SkillBar::SkillClickCallBack));
skill_item->setPosition(CCPointZero);
skill_item->setUserObject(callback);
skill_item->setAnchorPoint(ccp(0.0f,0.5f));


skills=CCMenu::menuWithItems(skill_item,NULL);
skills->setPosition(CCPointZero);
skills->setAnchorPoint(ccp(0.0f,0.5f));
this->addChild(skills);

cellwidth=normal->getTexture()->getContentSize().width;
cellheight=normal->getTexture()->getContentSize().height;

progressTimer= CCProgressTimer::progressWithSprite(bg);
progressTimer->setPosition(CCPointZero);
progressTimer->setTag(count);
progressTimer->setAnchorPoint(ccp(0.0f,0.5f));
char tmpstr[32];
memset(tmpstr,'\0',32);
sprintf(tmpstr,"%d",count);
putFloat(tmpstr,durtaion);

skill_item->setUserData(progressTimer);
this->addChild(progressTimer);
progressTimer->setVisible(false);

this->setContentSize(CCSizeMake((count+1)*cellwidth,cellheight));
}

/*
*@description: 设置技能条背景
*@author: buerkai.iteye.com
*@fullName: SkillBar::setSkillBarBg
*@return: void
*@param1: bg,技能背景的sprite
*/
void SkillBar::setSkillBarBg(CCSprite *bg){
CCSize size=this->getContentSize();
CCSize bgsize=bg->getContentSize();
bg->setScaleX(size.width/bgsize.width);
bg->setScaleY(size.height/bgsize.height);
this->addChild(bg,-1);
bg->setPosition(CCPointZero);
bg->setAnchorPoint(ccp(0.0f,0.5f));

}



/*
*@description: 添加一个技能按钮
*@author: buerkai.iteye.com
*@fullName: SkillBar::addSkillBar
*@return: SkillBar
*@param1: normal,技能按键正常情况的sprite
*@param2: select,技能按键选中情况的sprite
*@param3: bg,技能按键冷却情况的sprite
*@param4: durtaion,冷却时间
*@param5: callback,点击技能按键后的回调函数
*/
void SkillBar::addSkillBar(CCSprite *normal,CCSprite *select,CCSprite *bg,float durtaion,CCCallFunc* callback){
count++;
skill_item=CCMenuItemSprite::itemWithNormalSprite(normal,select,normal,this,menu_selector(SkillBar::SkillClickCallBack));
skill_item->setUserObject(callback);
skills->addChild(skill_item);
skill_item->setPosition(ccp(count *cellwidth,0));
skill_item->setAnchorPoint(ccp(0.0f,0.5f));

progressTimer= CCProgressTimer::progressWithSprite(bg);
progressTimer->setPosition(ccp(count *cellwidth,0));
progressTimer->setTag(count);
progressTimer->setAnchorPoint(ccp(0.0f,0.5f));
char tmpstr[32];
memset(tmpstr,'\0',32);
sprintf(tmpstr,"%d",count);
putFloat(tmpstr,durtaion);

this->addChild(progressTimer);
progressTimer->setVisible(false);
skill_item->setUserData(progressTimer);


this->setContentSize(CCSizeMake((count+1)*cellwidth,cellheight));
}



/*
*@description: 技能按键回调函数
*@author: buerkai.iteye.com
*@fullName: SkillBar::SkillClickCallBack
*@return: void
*@param1: object,回调函数返回的CCMenuItemSprite *
*/
void SkillBar::SkillClickCallBack(CCObject* object){
skill_item=(CCMenuItemSprite*)object;
skill_item->setEnabled(false);

progressTimer=(CCProgressTimer*)skill_item->getUserData();
progressTimer->setType(kCCProgressTimerTypeRadial);
progressTimer->setVisible(true);

int id=progressTimer->getTag();
char tmpstr[32];
memset(tmpstr,'\0',32);
sprintf(tmpstr,"%d",id);
float duration=getFloat(tmpstr);

CCCallFuncND* action_call_back = CCCallFuncND::actionWithTarget(this,callfuncND_selector(SkillBar::SkillCoolDownCallBack),(void *)object);
action_progress=CCProgressTo::actionWithDuration(duration, 100);
progressTimer->runAction(CCSequence::actions(action_progress,action_call_back,NULL));
CCCallFunc *back=(CCCallFunc*)skill_item->getUserObject();
back->execute();

}


/*
*@description: 技能冷却完成时的回调
*@author: buerkai.iteye.com
*@fullName: SkillBar::SkillCoolDownCallBack
*@return: void
*@param1: node,回调函数返回的CCNode *
*@param2: object,回调函数返回的CCMenuItemSprite *
*/
void SkillBar::SkillCoolDownCallBack(CCNode* node,void *object){
skill_item=(CCMenuItemSprite*)object;
progressTimer=(CCProgressTimer*)skill_item->getUserData();
skill_item->setEnabled(true);
progressTimer->setVisible(false);
}


转载请注明出处:http://buerkai.iteye.com

你可能感兴趣的:(cocos2d)