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
|
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC;
class
GameScene :
public
cocos2d::Layer,
public
EditBoxDelegate
{
public
:
static
cocos2d::Scene* createScene();
virtual
bool
init();
CREATE_FUNC(GameScene);
//输入委托
void
editBoxEditingDidBegin(EditBox *editBox);
void
editBoxEditingDidEnd(EditBox *editBox);
void
editBoxTextChange(EditBox *editBox,
const
std::string &text);
void
editBoxReturn(EditBox *editBox);
//滑杆回调
void
sliderCallback(Ref *sender, Control::EventType controlEvent);
private
:
//注意不能用auto关键字
Size size;
Sprite *sprite;
};
|
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
|
// Created by Jacedy on 14-8-11.
#include "GameScene.h"
USING_NS_CC;
cocos2d::Scene* GameScene::createScene()
{
auto
scene = Scene::create();
//创建一个场景
auto
layer = GameScene::create();
//创建一个图层
scene->addChild(layer);
return
scene;
}
//初始化当前的图层
bool
GameScene::init()
{
if
(!Layer::init())
//初始化父类
return
false
;
//获取屏幕大小
size = Director::getInstance()->getVisibleSize();
//auto size = Director::getInstance()->getWinSize();
Point origin = Director::getInstance()->getVisibleOrigin();
auto
label = Label::createWithSystemFont(
"Hello World"
,
"Mark Felt"
, 25);
label->setPosition(Point(origin.x + size.width / 2, origin.y + size.height - label->getContentSize().height));
this
->addChild(label, 1);
//EditBox
auto
inputBox = Sprite::create(
"YellowSquare.png"
);
inputBox->setPosition(Point(size.width/2, size.height/3));
this
->addChild(inputBox, 1);
EditBox *editBox = EditBox::create(Size(100, 35.0), Scale9Sprite::create());
editBox->setPosition(Point(size.width/2, size.height/3));
//editBox->setPlaceHolder("请输入数字"); //设置editBox输入为空时的显示状态
editBox->setInputMode(EditBox::InputMode::NUMERIC);
//输入模式,这里设置为数字
editBox->setDelegate(
this
);
//开启委托
editBox->setFontColor(Color3B::BLACK);
editBox->setText(
"0"
);
editBox->setTag(10);
this
->addChild(editBox, 2);
//ControlSlider
auto
slide_control = ControlSlider::create(
"sliderProgress.png"
,
"sliderTrack.png"
,
"sliderThumb.png"
);
slide_control->setPosition(Point(size.width/2, size.height/2));
slide_control->setMinimumValue(0.0f);
//设置最小值
slide_control->setMaximumValue(100.0f);
//设置最大值
slide_control->setValue(0.0f);
//设置初始值
slide_control->setTag(20);
//设置拖动回调
slide_control->addTargetWithActionForControlEvents(
this
, cccontrol_selector(GameScene::sliderCallback), Control::EventType::VALUE_CHANGED);
this
->addChild(slide_control, 2);
return
true
;
}
void
GameScene::editBoxEditingDidBegin(EditBox *editBox)
{
}
void
GameScene::editBoxEditingDidEnd(EditBox *editBox)
{
}
void
GameScene::editBoxTextChange(EditBox *editBox,
const
std::string &text)
{
int
num_int = std::
atoi
(text.c_str());
//将字符串转成整型
int
maxNum = 100;
if
(num_int > maxNum) {
num_int = maxNum;
}
char
buf_str[16];
sprintf
(buf_str,
"%d"
, num_int);
editBox->setText(buf_str);
//重置editBox的值
//改变ControlSlider的值
auto
slide = (ControlSlider *)
this
->getChildByTag(20);
slide->setValue((
float
)num_int);
//重置slide的值
}
void
GameScene::editBoxReturn(EditBox *editBox)
{
}
void
GameScene::sliderCallback(Ref *sender, Control::EventType controlEvent)
{
auto
slide_control = (ControlSlider *)sender;
//通过回调参数sender获得ControlSlider
int
current_value = slide_control->getValue();
//获得slide当前的值
char
buf_str[16];
sprintf
(buf_str,
"%d"
, current_value);
auto
editBox = (EditBox *)
this
->getChildByTag(10);
editBox->setText(buf_str);
}
|