forked from XingMo/ARPG2D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkillButton.cpp
110 lines (92 loc) · 2.86 KB
/
SkillButton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "SkillButton.h"
USING_NS_CC;
SkillButton::SkillButton() :
mItemSkill(NULL),
mMenuSkill(NULL),
mStencil(NULL),
mProgressTimer(NULL),
mCDTime(1.f)
{
}
SkillButton::~SkillButton()
{
}
SkillButton* SkillButton::createSkillButton(float cdTime, const char* stencil_file_name, const char* button_normal_name, const char* button_click_name, Skill_type type)
{
SkillButton* skillButton = new SkillButton();
if (skillButton && skillButton->init(cdTime, stencil_file_name, button_normal_name, button_click_name, type))
{
skillButton->autorelease();
return skillButton;
}
else
{
delete skillButton;
skillButton = NULL;
}
return NULL;
}
bool SkillButton::init(float cdTime, const char* stencil_file_name, const char* button_normal_name, const char* button_click_name, Skill_type type)
{
//在底层添加技能按钮
mItemSkill = MenuItemImage::create(button_normal_name, button_click_name, [=](Ref *){
//使用技能
switch (type){
case JUMP:
hero->actJump();
break;
default:
break;
}
});
mItemSkill->setPosition(CCPointZero);
mMenuSkill = CCMenu::create(mItemSkill, NULL);
mMenuSkill->setPosition(CCPointZero);
addChild(mMenuSkill, -100);
//在中间层添加阴影模版
mStencil = CCSprite::create(stencil_file_name);
mStencil->setPosition(CCPointZero);
mStencil->setVisible(false);
addChild(mStencil);
//在最上层添加旋转进度条精灵
CCSprite* progressSprite = CCSprite::create(button_normal_name);
mProgressTimer = CCProgressTimer::create(progressSprite);
mProgressTimer->setPosition(CCPointZero);
mProgressTimer->setVisible(false);
addChild(mProgressTimer, 100);
mCDTime = cdTime;
return true;
}
void SkillButton::skillClickCallBack(cocos2d::CCObject* obj)
{
//冷却计时
mItemSkill->setEnabled(false);
//显示蒙版
mStencil->setVisible(true);
//设置精灵进度条为顺时针
mProgressTimer->setVisible(true);
mProgressTimer->setType(kCCProgressTimerTypeRadial);
//技能冷却动画
CCActionInterval* action_progress_to = CCProgressTo::create(mCDTime, 100);
CCCallFunc* action_callback = CCCallFuncN::create(this, callfuncN_selector(SkillButton::skillCoolDownCallBack));
mProgressTimer->runAction(CCSequence::create(action_progress_to, action_callback, NULL));
}
void SkillButton::skillCoolDownCallBack(CCNode* node)
{
//冷却结束后将各种冷却时的精灵隐藏
//蒙板不可见
mStencil->setVisible(false);
//进度条精灵不可见
mProgressTimer->setVisible(false);
//技能按钮变为可用状态
mItemSkill->setEnabled(true);
}
void SkillButton::setHero(Hero* h){
this->hero = h;
}
//添加技能按钮
//以下代码加入到场景的init
//SkillButton* mSkillButton = SkillButton::createSkillButton(2.f, "Sprite-0008.png", "CloseNormal.png", "CloseSelected.png", JUMP);
//mSkillButton->setHero(hero);
//mSkillButton->setPosition(VISIBLE_SIZE.width * 2 / 3, VISIBLE_SIZE.height / 3);
//addChild(mSkillButton);